Chapter 6 object-oriented Programmingobject Creationthe constructor pattern
- Object created by the constructor function that using new key would automatically have a constructor prop Erty (non-enumerable) which points to the constructor
How prototype
- Whenever a function is Created,its prototype property is also created. The prototype have a constructor property, that points, the function
- Each time the constructor are called to create a new instance, that instance have a internal pointer to the constructor ' S prototype. In ECMA-262 Fifth Edition, this is called [[Prototype]]. There is no standard-of-access [[[Prototype]] from script, but Firefox, Safari and Chrome All-support a property on eve Ry object called Proto.; In the other implementations, the "is" completely hidden from script. The important thing to understand are that a direct link exists between the instance and the constructor ' s prototype BU T not between the instance and the constructor.
isprototypeof () method used to determine if [[Prototype]] points to the constructor ' s Prototype
alert(Person.prototype.isPrototypeOf(person1)); //truealert(Person.prototype.isPrototypeOf(person2)); //true
ECMAScript 5 Adds a method called object.getprototypeof (), which returns the value of [[Prototype]]. This method was supported in Internet Explorer 9+,firefox 3.5+, Safari 5+, Opera 12+, and Chrome.
alert(Object.getPrototypeOf(person1) == Person.prototype); //truealert(Object.getPrototypeOf(person1).name); //”Nicholas”
- It's possible to read values on the prototype from object instance but it's not possible to overwrite them. If you add a property of the same name as a property on the prototype, the new property just masks the property on T He prototype. We can use the delete operator to remove the property on the instance so, the property on the prototype. The same name can be access again.
- The hasownproperty method used to determine if a property exists on the instance or the prototype (inherited from Object)
- The ECMAScript 5 object.getownpropertydescriptor () works on own properties.
Prototypes and the
inchoperator
- The in operator return true if the property of given name can be access from the object (no matter it's on The object itself oron the prototype).
- The for-in returns all properties, that is accessible and enumerable by the object (include properties on the Pro ToType)
- ECMAScript 5 Object.keys () method:accepts as its argument and returns an array of strings containing the names of All and enumerable properties.
- object.getownpropertynames ()returns all instance properties, whether enumerable or not
- (methods is supported in Internet Explorer +, Firefox 4+, Safari 5+, Opera 12+, and Chrome.)
Professional JavaScript for Web developers Reading notes