Professional JavaScript for Web Developers 讀書筆記

來源:互聯網
上載者:User

標籤:

Chapter 6 OBJECT-ORIENTED PROGRAMMINGObject Creationthe 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)); //truealert(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); //truealert(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 讀書筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.