6.1 Understanding Objects
var person = new Object ();
Person.name = "Nicholas";
person.age = 29;
Person.job = "Software Engineer";
Person.sayname = function () {
alert (this.name);
};
Person.sayname ();
6.1.1 Property Type 1. Data properties
- [[Configurable]]: Indicates whether the property can be redefined by deleting the property from Delete, can modify the attribute's attributes, or can modify the property to an accessor property.
- [[Enumerable]]: Indicates whether the property can be returned through a for-in loop.
- [[writable]]: Indicates whether the value of the property can be modified.
- [[Value]]: Contains the data value for this property. When reading a property value, read from this location, and when writing the property, save the new value in this position. The default value is undefined
2. Accessor Properties
- [[Configureable]]: Indicates whether the property can be redefined by deleting the property from Delete, can modify the attribute's attributes, or can modify the property to a data property.
- [[Enumerable]]: Indicates whether the property can be returned through a for-in loop.
- [[Get]]: The function that is called when the property is read. The default value is undefinded.
- [[Set]]: a function that is called when a property is written. The default value is undefinded.
6.1.2 Defining multiple properties
varBook = {}; Object.defineproperties (book, {_year: {value:2004}, Edition: {value:1}, Year: {get:function(){ return This. _year; }, set:function(newvalue) {if(NewValue > 2004) { This. _year =NewValue; This. edition + = newValue-2004; } } } }); Book.year= 2005; alert (book.edition); //2
6.1.3 Properties of Read attributes
varBook = {}; Object.defineproperties (book, {_year: {value:2004}, Edition: {value:1}, Year: {get:function(){ return This. _year; }, set:function(newvalue) {if(NewValue > 2004) { This. _year =NewValue; This. edition + = newValue-2004; } } } }); varDescriptor = object.getownpropertydescriptor (book, "_year"); alert (Descriptor.value); //2004alert (descriptor.configurable);//falseAlerttypeofDescriptor.get);//"undefined" varDescriptor = object.getownpropertydescriptor (book, ' Year '); alert (Descriptor.value); //undefinedalert (descriptor.enumerable);//falseAlerttypeofDescriptor.get);//"function"
6.2.1 Factory mode
functionCreateperson (name, age, job) {varo =NewObject (); O.name=name; O.age=Age ; O.job=job; O.sayname=function() {alert ( This. Name); }; returno; } varPerson1 = Createperson ("Nicholas", "Software Engineer"); varPerson2 = Createperson ("Greg", "Doctor"); Person1.sayname (); //"Nicholas"Person2.sayname ();//"Greg"
6.2.2 Constructor Mode
functionPerson (name, age, job) { This. Name =name; This. Age =Age ; This. Job =job; This. Sayname =function() {alert ( This. Name); }; } varPerson1 =NewPerson ("Nicholas", "Software Engineer"); varPerson2 =NewPerson ("Greg", "Doctor"); Person1.sayname (); //"Nicholas"Person2.sayname ();//"Greg"Alert (Person1instanceofObject);//trueAlert (Person1instanceofperson);//trueAlert (Person2instanceofObject);//trueAlert (Person2instanceofperson);//trueAlert (Person1.constructor= = person);//trueAlert (Person2.constructor = = person);//trueAlert (Person1.sayname= = Person2.sayname);//false
6.2.3 Prototype mode
functionPerson () {} Person.prototype.name= "Nicholas"; Person.prototype.age= 29; Person.prototype.job= "Software Engineer"; Person.prototype.sayName=function() {alert ( This. Name); }; varPerson1 =NewPerson (); Person1.sayname (); //"Nicholas" varPerson2 =NewPerson (); Person2.sayname (); //"Nicholas"Alert (Person1.sayname= = Person2.sayname);//truealert (Person.prototype.isPrototypeOf (Person1)); //trueAlert (Person.prototype.isPrototypeOf (Person2));//true //Only works if Object.getprototypeof () is available if(object.getprototypeof) {alert (object.getprototypeof (Person1)= = Person.prototype);//trueAlert (object.getprototypeof (person1). Name);//"Nicholas"}
1. Understanding Prototypes
Whenever a new function is created, a prototype property is created for the function based on a specific set of rules. By default, all prototype properties automatically get a constructor property that contains a pointer to the prototype property function.
2. Prototypes and in Operators
Two occasions where the in operator is used: Single or for-in loops
Used alone: The In operator returns True when the given property is accessible through the object
3. Simpler prototype syntax
function person () {}
Person.prototype = {
Name: ' Nicholas ',
Age:29,
Sayname:function () {
alert (this.name);
}
}
4. The dynamic nature of the prototype
Rewriting the prototype will cut off the connection between the existing prototype and any previously existing object instances, although they still refer to the original prototype
5. Prototypes of native objects
Methods that can extend native objects
String.prototype.startWith = function () {
return This.indexof (text) = = 0;
}
var msg = ' Hello world ';
Alert (msg.startwith (' Hello ')); Output true
6. Problems with prototype objects
All properties in the prototype are shared by many instances, which is not optimistic for attributes that contain reference types.
6.3.1 prototype chain
The prototype chain implements the principle of inheritance: Use a prototype to have one reference type inherit the properties and methods of another reference type. Review the relationship of a constructor, prototype, and instance: each constructor has a prototype object, and the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object. So what happens if we let the prototype object be equal to another type of implementation? Obviously, the prototype object at this point will contain a pointer to another prototype, and a pointer to another constructor is also included in the other prototype. If another prototype is another type of instance, then the above relationship is established. The progression of such layers constitutes the chain of examples and prototypes.
JavaScript Advanced Programming (Third edition) Chapter sixth object-oriented programming