码迷,mamicode.com
首页 > 编程语言 > 详细

Professional JavaScript for Web Developers 读书笔记

时间:2015-03-31 06:20:30      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

Chapter 6 OBJECT-ORIENTED PROGRAMMING

Object Creation

the constructor pattern

  • Object created by the constructor function that using new key will automatically have a constructor property(non-enumerable) which points to the constructor

How prototype work

  • Whenever a function is created,its prototype property is also created. The prototype has a constructor property that points back to the function
  • Each time the constructor is called to create a new instance, that instance has a internal pointer to the constructor‘s prototype.In ECMA-262 fifth edition, this is called [[Prototype]]. There is no standard way to access [[Prototype]] from script, but Firefox, Safari and Chrome all support a property on every object called proto.; in other implementations, this property is completely hidden from script. The important thing to understand is that a direct link exists between the instance and the constructor‘s prototype but 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)); //true
alert(Person.prototype.isPrototypeOf(person2)); //true

ECMAScript 5 adds a method called Object.getPrototypeOf(), which returns the value of [[Prototype]].This method is supported in Internet Explorer 9+,Firefox 3.5+, Safari 5+, Opera 12+, and Chrome.

alert(Object.getPrototypeOf(person1) == Person.prototype); //true
alert(Object.getPrototypeOf(person1).name); //”Nicholas”

  • It‘s possible to read values on the prototype from object instance but it is not possible to overwrite them. If you add a property that has the same name as a property on the prototype, the new property just masks the property on the prototype. We can use delete operator to remove the property on the instance so that the property on the prototype that with 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 in operator

  • the in operator return true when the property of given name can be access from the object(no matter it is on the object itself or on the prototype).
  • The for-in returns all properties that are accessible and enumerable by the object (include properties on the prototype)

  • 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 are supported in Internet Explorer 9+, Firefox 4+, Safari 5+, Opera 12+, and Chrome.)

Professional JavaScript for Web Developers 读书笔记

标签:

原文地址:http://www.cnblogs.com/deryck/p/4379666.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!