SlideShare a Scribd company logo
Ian
 JavaScript Objects Are Dictionaries
 In JavaScript, objects are just collections of name/value
pairs—think of a JavaScript object as a dictionary with
string keys.
 We can get and set the properties of an object using
either the familiar "." (dot) operator, or the "[]"
operator, which is typically used when dealing with a
dictionary.
 var userObject = new Object();
 var userObject = {};
 userObject.lastLoginTime = new Date();
 userObject["lastLoginTime"] = new Date();
 alert(userObject.lastLoginTime);
 alert(userObject["lastLoginTime"]);
 This is how we usually define a function in JavaScript.
 function func(x) { alert(x); }
 func("blah");
 You can create an anonymous function object, and assign it to
variable func
 var func = function(x) { alert(x); };
 func("blah2");
 Or
 var func = new Function("x", "alert(x);");
 func("blah3");
 This shows that a function is really just an object that supports a
function call operation.
 function test() {
 Return “Hello”;
 }
 console.log(test);
 console.log(test());
 var add = function(x, y) {
return x + y;
};
 alert(add(1, 2)); // Displays "3”
 var result = (function(x, y) {
return x + y;
})(1, 2);
alert(result); // Displays "3"
 function displayQuote() {
 alert(this.memorableQuote);
 }
 var oscarWilde = {
 "memorableQuote": "True friends stab you in the front. "
 };
 displayQuote.call(oscarWilde);
 Remember, a function in JavaScript is an object. Every function object has a
method named call, which calls the function as a method of the first argument.
 That is, whichever object we pass into call as its first argument will become the
value of "this" in the function invocation.
var myDog = {
"name" : "Spot",
"bark" : function() { alert("Woof! "); },
"displayFullName" : function() {
alert(this.name + " The Alpha Dog"); },
"chaseMrPostman" : function() {
// implementation beyond the scope of this article
}
};
myDog.displayFullName();
myDog.bark(); // Woof!
 Constructor Functions but No Classes
 function DogConstructor(name) {
 this.name = name;
 this.respondTo = function(name) {
 if(this.name == name) {
 alert("Woof");
}
};
 }
 var spot = new DogConstructor("Spot");
spot.respondTo("Rover"); // …
 spot.respondTo("Spot"); // Woof
 var spot = new DogConstructor("Spot");
 First, it creates a new empty object. Then, the function
call that immediately follows is executed, with the new
empty object set as the value of "this" within that
function.
 // create an empty object
 var spot = {};
 // call the function as a method of the empty object
DogConstructor.call(spot, "Spot");
 In fact, usually in JavaScript the name of the constructor
function is the name of the class you're simulating.
 // Think of this as class Dog
 function Dog(name) {
 // instance variable
 this.name = name;
 // instance method? Hmmm...
 this.respondTo = function(name) {
 if(this.name == name) { alert(“Woof”); }
 };
 }
 var spot = new Dog(“Spot”);
 The name comes from the idea that in JavaScript, an
object is created as a copy of an existing example (that
is, a prototype) object.
 Any properties and methods of this prototype object
will appear as properties and methods of the objects
created from that prototype's constructor.
 You can say that these objects inherit their properties
and methods from their prototype.
 var buddy = new Dog(“Buddy“);
 the object referenced by buddy inherits properties and
methods from its prototype
 Although it's probably not obvious from just that one
line where the prototype comes from.
Ian 20150116 java script oop
 In JavaScript, every function has a property named
"prototype" that refers to a prototype object.
 This prototype object in turn has a property named
"constructor," which refers back to the function itself.
It's sort of a circular reference;
Ian 20150116 java script oop
 function GreatDane() { }
 var rover = new GreatDane();
 var spot = new GreatDane();
 GreatDane.prototype.getBreed = function() {
 return "Great Dane";
 };
 alert(rover.getBreed()); // Great Dane
 spot.getBreed = function() {
 return "Little Great Dane";
 };
 alert(spot.getBreed()); // Little Great Dane
 alert(rover.getBreed()); // Great Dane
 function DateTime() { }

 // set static method now()
 DateTime.now = function() {
 return new Date();
 };
 A closure is a runtime phenomenon that comes about when an
inner function (or in C#, an inner anonymous method) is bound
to the local variables of its outer function.
 <script>
 var add = (function () {
 var counter = 0;
 return function () {return counter += 1;}
 })();
 function myFunction(){
 document.getElementById("demo").innerHTML = add();
 }
 </script>
 function Person(name, age) {
 this.getName = function() { return name; };
 this.setName = function(newName) { name = newName; };
 this.getAge = function() { return age; };
 this.setAge = function(newAge) { age = newAge; };
 }
 var ray = new Person("Ray", 31);
 // class Pet
 function Pet(name) {
 this.getName = function() { return name; };
 this.setName = function(newName) { name = newName; };
 }
 Pet.prototype.toString = function() {
 return "This pet’s name is: " + this.getName();
 };
 // end of class Pet
 var parrotty = new Pet("Parrotty the Parrot");
 alert(parrotty);
 // class Dog : Pet
 // public Dog(string name, string breed)
 function Dog(name, breed) {
 // think Dog : base(name)
 Pet.call(this, name);
 this.getBreed = function() { return breed; };
 // Breed doesn't change, obviously! It's read only.
 // this.setBreed = function(newBreed) { name =
newName; };
 }
 // this makes Dog.prototype inherits
 // from Pet.prototype
 Dog.prototype = new Pet();
 // remember that Pet.prototype.constructor
 // points to Pet. We want our Dog instances'
 // constructor to point to Dog.
Dog.prototype.constructor = Dog;
 // Now we override Pet.prototype.toString
Dog.prototype.toString = function() {
 return "This dog's name is: " + this.getName() +
 ", and its breed is: " + this.getBreed();
 };
 // end of class Dog
 var dog = new Dog("Buddy", "Great Dane");
 // test the new toString()
 alert(dog); // This dog's name is: Buddy, and its breed is: Great Dane
 // Testing instanceof (similar to the is operator)
 // (dog is Dog)? yes
 alert(dog instanceof Dog);
 // (dog is Pet)? yes
 alert(dog instanceof Pet);
 // (dog is Object)?
 yes alert(dog instanceof Object);
 var MSDNMagNS = {};
 MSDNMagNS.Pet = function(name) { // code here };
MSDNMagNS.Pet.prototype.toString = function() {
 // code
 };
 var pet = new MSDNMagNS.Pet("Yammer");
 var MSDNMagNS = {};
 // nested namespace "Examples"
 MSDNMagNS.Examples = {}; MSDNMagNS.Examples.Pet
= function(name) {
 // code
 };
 MSDNMagNS.Examples.Pet.prototype.toString =
function() {
 // code
 };
 var pet = new MSDNMagNS.Examples.Pet("Yammer");
 // MSDNMagNS.Examples and Pet definition...
 // think "using Eg = MSDNMagNS.Examples;"
 var Eg = MSDNMagNS.Examples;
 var pet = new Eg.Pet("Yammer");
 alert(pet);

More Related Content

What's hot (20)

PDF
Jggug 2010 330 Grails 1.3 観察
Tsuyoshi Yamamoto
 
PDF
Object Oriented JavaScript
Michael Girouard
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
PDF
The many facets of code reuse in JavaScript
Leonardo Borges
 
KEY
JavaScript Classes and Inheritance
marcheiligers
 
KEY
Javascript tid-bits
David Atchley
 
PDF
JS Level Up: Prototypes
Vernon Kesner
 
PDF
Google guava
t fnico
 
PPTX
5 Tips for Better JavaScript
Todd Anglin
 
PDF
Introduction into ES6 JavaScript.
boyney123
 
PPTX
Advanced JavaScript Concepts
Naresh Kumar
 
PDF
Apache Commons - Don\'t re-invent the wheel
tcurdt
 
PDF
ES2015 (ES6) Overview
hesher
 
PDF
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
PDF
3. Объекты, классы и пакеты в Java
DEVTYPE
 
PPT
Object Oriented JavaScript
Donald Sipe
 
PDF
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Kenji Tanaka
 
PDF
Java 8 Stream API. A different way to process collections.
David Gómez García
 
PDF
Javascript: the important bits
Chris Saylor
 
PDF
EMFPath
mikaelbarbero
 
Jggug 2010 330 Grails 1.3 観察
Tsuyoshi Yamamoto
 
Object Oriented JavaScript
Michael Girouard
 
Advanced JavaScript
Stoyan Stefanov
 
The many facets of code reuse in JavaScript
Leonardo Borges
 
JavaScript Classes and Inheritance
marcheiligers
 
Javascript tid-bits
David Atchley
 
JS Level Up: Prototypes
Vernon Kesner
 
Google guava
t fnico
 
5 Tips for Better JavaScript
Todd Anglin
 
Introduction into ES6 JavaScript.
boyney123
 
Advanced JavaScript Concepts
Naresh Kumar
 
Apache Commons - Don\'t re-invent the wheel
tcurdt
 
ES2015 (ES6) Overview
hesher
 
Explaining ES6: JavaScript History and What is to Come
Cory Forsyth
 
3. Объекты, классы и пакеты в Java
DEVTYPE
 
Object Oriented JavaScript
Donald Sipe
 
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Kenji Tanaka
 
Java 8 Stream API. A different way to process collections.
David Gómez García
 
Javascript: the important bits
Chris Saylor
 
EMFPath
mikaelbarbero
 

Viewers also liked (18)

PPTX
Vic weekly learning_20151120
LearningTech
 
PPTX
flexbox report
LearningTech
 
PPTX
Peggy markdown
LearningTech
 
PPT
Bootstrap–component
LearningTech
 
PPTX
What is new in visual studio 2015
LearningTech
 
PPT
20121228 jqueryui - button - By Nat
LearningTech
 
PPT
20120601_Excel 元件 ep plus joncash
LearningTech
 
PPT
20121228 jQueryui - datepicker - By Jason
LearningTech
 
PPT
20120601_jquery tree traversal_drake
LearningTech
 
PPT
JSRender
LearningTech
 
PPT
Covariance, contravariance 觀念分享
LearningTech
 
PPTX
Mvc route
LearningTech
 
PPTX
TypeScript by Howard
LearningTech
 
PPT
React.js 20150828
LearningTech
 
PPT
Power shell object
LearningTech
 
PPTX
Expression tree
LearningTech
 
PPTX
PostCss
LearningTech
 
PPTX
ReactJs
LearningTech
 
Vic weekly learning_20151120
LearningTech
 
flexbox report
LearningTech
 
Peggy markdown
LearningTech
 
Bootstrap–component
LearningTech
 
What is new in visual studio 2015
LearningTech
 
20121228 jqueryui - button - By Nat
LearningTech
 
20120601_Excel 元件 ep plus joncash
LearningTech
 
20121228 jQueryui - datepicker - By Jason
LearningTech
 
20120601_jquery tree traversal_drake
LearningTech
 
JSRender
LearningTech
 
Covariance, contravariance 觀念分享
LearningTech
 
Mvc route
LearningTech
 
TypeScript by Howard
LearningTech
 
React.js 20150828
LearningTech
 
Power shell object
LearningTech
 
Expression tree
LearningTech
 
PostCss
LearningTech
 
ReactJs
LearningTech
 
Ad

Similar to Ian 20150116 java script oop (20)

PPTX
Object Oriented JavaScript
Julie Iskander
 
PDF
JavaScript Survival Guide
Giordano Scalzo
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
DOC
Jsphp 110312161301-phpapp02
Seri Moth
 
PPT
OO JS for AS3 Devs
Jason Hanson
 
PDF
Advanced javascript
Doeun KOCH
 
PDF
Javascript
Aditya Gaur
 
PDF
JavaScript for PHP developers
Stoyan Stefanov
 
PDF
Java script object model
James Hsieh
 
PDF
JavaScript Inheritance
Jussi Pohjolainen
 
PPTX
JavaScript - i och utanför webbläsaren (2010-03-03)
Anders Jönsson
 
PDF
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
PPTX
Advanced JavaScript
Nascenia IT
 
PDF
The Beauty Of Java Script V5a
rajivmordani
 
PDF
JavaScript - Like a Box of Chocolates
Robert Nyman
 
PPT
Web Optimization Summit: Coding for Performance
johndaviddalton
 
PDF
The Beauty of Java Script
Michael Girouard
 
DOCX
Link.javaLink.javapackage com.bookstore.domain.model;import .docx
SHIVA101531
 
PDF
Js objects
anubavam-techkt
 
PDF
Javascript development done right
Pawel Szulc
 
Object Oriented JavaScript
Julie Iskander
 
JavaScript Survival Guide
Giordano Scalzo
 
TypeScript Introduction
Dmitry Sheiko
 
Jsphp 110312161301-phpapp02
Seri Moth
 
OO JS for AS3 Devs
Jason Hanson
 
Advanced javascript
Doeun KOCH
 
Javascript
Aditya Gaur
 
JavaScript for PHP developers
Stoyan Stefanov
 
Java script object model
James Hsieh
 
JavaScript Inheritance
Jussi Pohjolainen
 
JavaScript - i och utanför webbläsaren (2010-03-03)
Anders Jönsson
 
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
Advanced JavaScript
Nascenia IT
 
The Beauty Of Java Script V5a
rajivmordani
 
JavaScript - Like a Box of Chocolates
Robert Nyman
 
Web Optimization Summit: Coding for Performance
johndaviddalton
 
The Beauty of Java Script
Michael Girouard
 
Link.javaLink.javapackage com.bookstore.domain.model;import .docx
SHIVA101531
 
Js objects
anubavam-techkt
 
Javascript development done right
Pawel Szulc
 
Ad

More from LearningTech (20)

PPTX
vim
LearningTech
 
PPTX
Docker
LearningTech
 
PPTX
Semantic ui
LearningTech
 
PPTX
node.js errors
LearningTech
 
PPTX
Process control nodejs
LearningTech
 
PPTX
Expression tree
LearningTech
 
PPTX
SQL 效能調校
LearningTech
 
PPTX
Vic weekly learning_20160504
LearningTech
 
PPTX
Reflection &amp; activator
LearningTech
 
PPTX
Node child process
LearningTech
 
PPTX
20160415ken.lee
LearningTech
 
PPTX
Peggy elasticsearch應用
LearningTech
 
PPTX
Vic weekly learning_20160325
LearningTech
 
PPTX
D3js learning tips
LearningTech
 
PPTX
git command
LearningTech
 
PDF
Asp.net MVC DI
LearningTech
 
PPTX
Vic weekly learning_20151127
LearningTech
 
PPTX
Mocha.js
LearningTech
 
PPTX
R language
LearningTech
 
PPTX
20151120 ian cocos2d js
LearningTech
 
Docker
LearningTech
 
Semantic ui
LearningTech
 
node.js errors
LearningTech
 
Process control nodejs
LearningTech
 
Expression tree
LearningTech
 
SQL 效能調校
LearningTech
 
Vic weekly learning_20160504
LearningTech
 
Reflection &amp; activator
LearningTech
 
Node child process
LearningTech
 
20160415ken.lee
LearningTech
 
Peggy elasticsearch應用
LearningTech
 
Vic weekly learning_20160325
LearningTech
 
D3js learning tips
LearningTech
 
git command
LearningTech
 
Asp.net MVC DI
LearningTech
 
Vic weekly learning_20151127
LearningTech
 
Mocha.js
LearningTech
 
R language
LearningTech
 
20151120 ian cocos2d js
LearningTech
 

Recently uploaded (20)

PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Digital Circuits, important subject in CS
contactparinay1
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 

Ian 20150116 java script oop

  • 1. Ian
  • 2.  JavaScript Objects Are Dictionaries  In JavaScript, objects are just collections of name/value pairs—think of a JavaScript object as a dictionary with string keys.  We can get and set the properties of an object using either the familiar "." (dot) operator, or the "[]" operator, which is typically used when dealing with a dictionary.
  • 3.  var userObject = new Object();  var userObject = {};  userObject.lastLoginTime = new Date();  userObject["lastLoginTime"] = new Date();  alert(userObject.lastLoginTime);  alert(userObject["lastLoginTime"]);
  • 4.  This is how we usually define a function in JavaScript.  function func(x) { alert(x); }  func("blah");  You can create an anonymous function object, and assign it to variable func  var func = function(x) { alert(x); };  func("blah2");  Or  var func = new Function("x", "alert(x);");  func("blah3");  This shows that a function is really just an object that supports a function call operation.
  • 5.  function test() {  Return “Hello”;  }  console.log(test);  console.log(test());
  • 6.  var add = function(x, y) { return x + y; };  alert(add(1, 2)); // Displays "3”  var result = (function(x, y) { return x + y; })(1, 2); alert(result); // Displays "3"
  • 7.  function displayQuote() {  alert(this.memorableQuote);  }  var oscarWilde = {  "memorableQuote": "True friends stab you in the front. "  };  displayQuote.call(oscarWilde);  Remember, a function in JavaScript is an object. Every function object has a method named call, which calls the function as a method of the first argument.  That is, whichever object we pass into call as its first argument will become the value of "this" in the function invocation.
  • 8. var myDog = { "name" : "Spot", "bark" : function() { alert("Woof! "); }, "displayFullName" : function() { alert(this.name + " The Alpha Dog"); }, "chaseMrPostman" : function() { // implementation beyond the scope of this article } }; myDog.displayFullName(); myDog.bark(); // Woof!
  • 9.  Constructor Functions but No Classes  function DogConstructor(name) {  this.name = name;  this.respondTo = function(name) {  if(this.name == name) {  alert("Woof"); } };  }  var spot = new DogConstructor("Spot"); spot.respondTo("Rover"); // …  spot.respondTo("Spot"); // Woof
  • 10.  var spot = new DogConstructor("Spot");  First, it creates a new empty object. Then, the function call that immediately follows is executed, with the new empty object set as the value of "this" within that function.  // create an empty object  var spot = {};  // call the function as a method of the empty object DogConstructor.call(spot, "Spot");
  • 11.  In fact, usually in JavaScript the name of the constructor function is the name of the class you're simulating.  // Think of this as class Dog  function Dog(name) {  // instance variable  this.name = name;  // instance method? Hmmm...  this.respondTo = function(name) {  if(this.name == name) { alert(“Woof”); }  };  }  var spot = new Dog(“Spot”);
  • 12.  The name comes from the idea that in JavaScript, an object is created as a copy of an existing example (that is, a prototype) object.  Any properties and methods of this prototype object will appear as properties and methods of the objects created from that prototype's constructor.  You can say that these objects inherit their properties and methods from their prototype.
  • 13.  var buddy = new Dog(“Buddy“);  the object referenced by buddy inherits properties and methods from its prototype  Although it's probably not obvious from just that one line where the prototype comes from.
  • 15.  In JavaScript, every function has a property named "prototype" that refers to a prototype object.  This prototype object in turn has a property named "constructor," which refers back to the function itself. It's sort of a circular reference;
  • 17.  function GreatDane() { }  var rover = new GreatDane();  var spot = new GreatDane();  GreatDane.prototype.getBreed = function() {  return "Great Dane";  };  alert(rover.getBreed()); // Great Dane
  • 18.  spot.getBreed = function() {  return "Little Great Dane";  };  alert(spot.getBreed()); // Little Great Dane  alert(rover.getBreed()); // Great Dane
  • 19.  function DateTime() { }   // set static method now()  DateTime.now = function() {  return new Date();  };
  • 20.  A closure is a runtime phenomenon that comes about when an inner function (or in C#, an inner anonymous method) is bound to the local variables of its outer function.  <script>  var add = (function () {  var counter = 0;  return function () {return counter += 1;}  })();  function myFunction(){  document.getElementById("demo").innerHTML = add();  }  </script>
  • 21.  function Person(name, age) {  this.getName = function() { return name; };  this.setName = function(newName) { name = newName; };  this.getAge = function() { return age; };  this.setAge = function(newAge) { age = newAge; };  }  var ray = new Person("Ray", 31);
  • 22.  // class Pet  function Pet(name) {  this.getName = function() { return name; };  this.setName = function(newName) { name = newName; };  }  Pet.prototype.toString = function() {  return "This pet’s name is: " + this.getName();  };  // end of class Pet  var parrotty = new Pet("Parrotty the Parrot");  alert(parrotty);
  • 23.  // class Dog : Pet  // public Dog(string name, string breed)  function Dog(name, breed) {  // think Dog : base(name)  Pet.call(this, name);  this.getBreed = function() { return breed; };  // Breed doesn't change, obviously! It's read only.  // this.setBreed = function(newBreed) { name = newName; };  }
  • 24.  // this makes Dog.prototype inherits  // from Pet.prototype  Dog.prototype = new Pet();  // remember that Pet.prototype.constructor  // points to Pet. We want our Dog instances'  // constructor to point to Dog. Dog.prototype.constructor = Dog;
  • 25.  // Now we override Pet.prototype.toString Dog.prototype.toString = function() {  return "This dog's name is: " + this.getName() +  ", and its breed is: " + this.getBreed();  };  // end of class Dog
  • 26.  var dog = new Dog("Buddy", "Great Dane");  // test the new toString()  alert(dog); // This dog's name is: Buddy, and its breed is: Great Dane  // Testing instanceof (similar to the is operator)  // (dog is Dog)? yes  alert(dog instanceof Dog);  // (dog is Pet)? yes  alert(dog instanceof Pet);  // (dog is Object)?  yes alert(dog instanceof Object);
  • 27.  var MSDNMagNS = {};  MSDNMagNS.Pet = function(name) { // code here }; MSDNMagNS.Pet.prototype.toString = function() {  // code  };  var pet = new MSDNMagNS.Pet("Yammer");
  • 28.  var MSDNMagNS = {};  // nested namespace "Examples"  MSDNMagNS.Examples = {}; MSDNMagNS.Examples.Pet = function(name) {  // code  };  MSDNMagNS.Examples.Pet.prototype.toString = function() {  // code  };  var pet = new MSDNMagNS.Examples.Pet("Yammer");
  • 29.  // MSDNMagNS.Examples and Pet definition...  // think "using Eg = MSDNMagNS.Examples;"  var Eg = MSDNMagNS.Examples;  var pet = new Eg.Pet("Yammer");  alert(pet);

Editor's Notes

  • #5: 函數主體只是 Function 建構函式的 String 參數。這表示您可以在執行階段建構任意函數。 函數是物件,您可以在函數中設定或新增屬性,就像您對其他任何 JavaScript 物件所做的一樣:
  • #8: this它指向用來呼叫此方法的物件 請記住,JavaScript 中的函數是物件。每一個函數物件都有一個方法,稱為 call 我們以第一個引數傳入呼叫的任何物件,將成為函數呼叫的 "this" 值。這將是呼叫基底類別建構函式的技巧
  • #10: 但是在 JavaScript 中,根本沒有類別。最接近類別的方式,是定義如下的建構函式:
  • #11: "new" 運算子執行的動作很簡單。首先,它會建立新的空白物件。然後,會執行緊接在後面的函數呼叫 上面含有 "new" 運算子的這一行可視為類似下面這兩行: call([thisObj[, arg1[, arg2[, [, argN]]]]])
  • #13: 物件是以現有範例 (即原型) 物件的複本形式建立。此原型物件的任何屬性和方法,將視同從該原型建構函式所建立之物件的屬性和方法。您可以說這些物件是繼承其原型的屬性和方法。
  • #14: buddy 所參考的物件將繼承其原型的屬性和方法
  • #16: 在 JavaScript 中,每一個函數都有一個叫做 "prototype" 的屬性,此屬性會參考原型物件。原型物件則有一個叫做 "constructor" 的屬性,這會回頭參考函數本身。這是一種循環參考
  • #17: 當您嘗試存取物件的屬性/方法時,JavaScript 會檢查該屬性/方法是否定義在物件中。如果沒有,就會檢查該物件的原型。如果還是沒有,則會檢查該物件原型的原型,以此類推,直到 Object.prototype 為止。 您可以在 Dog.prototype 中定義 toString 方法,來覆寫 Object.prototype 的 toString 方法。
  • #20: 但 JavaScript 沒有 Private 成員的原生支援 (也沒有 Protected 成員)。任何人都可以存取物件的所有屬性和方法。有一個方法可以讓您在類別中擁有 Private 成員,但在這麼做之前,您必須先了解 Closure。
  • #21: Closure 是一個執行階段現象,當內部函數 (在 C# 中則為內部匿名方法) 繫結到其外部函數的區域變數時,就會產生此現象。 所謂的 Closure -- 因為內部函數會比定義該函數的環境 (也就是外部函數的引數和區域變數) 更早結束。
  • #22: 當 Person 一傳回,name 和 age 就應該永久消失。不過,它們會由四個內部函數擷取,雖然這些函數指派為 Person 執行個體的方法,實際上卻使 name 和 age 持續存活,但只能透過這四個方法存取。 ray.getName() // Ray 在 C# 中,類別的公用方法可存取其 Private 成員。但是在 JavaScript 中,Private 成員只能透過使這些 Private 成員包含在 Closure 內的方法加以存取
  • #25: 要把自己”指回”家 (利用 constructor)