prototype與__proto__區(qū)別詳細(xì)介紹
prototype與__proto__區(qū)別
Each constructor is a function that has a property named “prototype” that is used to implement prototype-based inheritance and shared properties. Every object created by a constructor has an implicit reference (called the object's prototype) to the value of its constructor's “prototype” property.
When a constructor creates an object, that object implicitly references the constructor's prototype property for the purpose of resolving property references. The constructor's prototype property can be referenced by the program expression constructor.prototype, and properties added to an object's prototype are shared, through inheritance, by all objects sharing the prototype. Alternatively, a new object may be created with an explicitly specified prototype by using the Object.create built-in function. –ECMAScript® 2015 Language Specification
__proto__是每個(gè)對(duì)象都有的一個(gè)屬性,而prototype是函數(shù)才會(huì)有的屬性!!!
使用Object.getPrototypeOf()代替__proto__!!!
一、prototype
幾乎所有的函數(shù)(除了一些內(nèi)建函數(shù))都有一個(gè)名為prototype(原型)的屬性,這個(gè)屬性是一個(gè)指針,指向一個(gè)對(duì)象,而這個(gè)對(duì)象的用途是包含可以有特定類型的所有實(shí)例共享的屬性和方法。prototype是通過(guò)調(diào)用構(gòu)造函數(shù)而創(chuàng)建的那個(gè)對(duì)象實(shí)例的原型對(duì)象。hasOwnProperty()判斷指定屬性是否為自有屬性;in操作符對(duì)原型屬性和自有屬性都返回true。
示例:自有屬性&原型屬性
var obj = {a: 1}; obj.hasOwnProperty("a"); // true obj.hasOwnProperty("toString"); // false "a" in obj; // true "toString" in obj; // true
示例:鑒別原型屬性
function hasPrototypeProperty(obj, name){ return name in obj && !obj.hasOwnProperty(name); }
二、__proto__
對(duì)象具有屬性__proto__,可稱為隱式原型,一個(gè)對(duì)象的隱式原型指向構(gòu)造該對(duì)象的構(gòu)造函數(shù)的原型,這也保證了實(shí)例能夠訪問(wèn)在構(gòu)造函數(shù)原型中定義的屬性和方法。
function Foo(){} var Boo = {name: "Boo"}; Foo.prototype = Boo; var f = new Foo(); console.log(f.__proto__ === Foo.prototype); // true console.log(f.__proto__ === Boo); // true Object.getPrototypeOf(f) === f.__proto__; // true
三、Object.getPrototypeOf()
一個(gè)對(duì)象實(shí)例通過(guò)內(nèi)部屬性[[Prototype]]跟蹤其原型對(duì)象。使用原型對(duì)象的好處是可以讓所有對(duì)象實(shí)例共享它所包含的屬性和方法??梢哉{(diào)用對(duì)象的Object.getPrototypeOf()方法讀取[[Prototype]]屬性的值,也可以使用isPrototypeOf()方法檢查某個(gè)對(duì)象是否是另一個(gè)對(duì)象的原型對(duì)象。大部分JavaScript引擎在所有對(duì)象上都支持一個(gè)名為_(kāi)_proto__的屬性,該屬性可以直接讀寫(xiě)[[Prototype]]屬性。
示例:原型對(duì)象
function Person(name) { this.name = name; } Person.prototype = { constructor: Person, sayName: function(){ console.log("my name is " + this.name); } } var p1 = new Person("ligang"); var p2 = new Person("Camile"); p1.sayName(); // my name is ligang p2.sayName(); // my name is Camile
While Object.prototype.proto is supported today in most browsers, its existence and exact behavior has only been standardized in the ECMAScript 6 specification as a legacy feature to ensure compatibility for web browsers. For better support, it is recommended that only Object.getPrototypeOf() be used instead. –MDN
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js中的hasOwnProperty和isPrototypeOf方法使用實(shí)例
- js中prototype用法詳細(xì)介紹
- JavaScript prototype 使用介紹
- JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的區(qū)別和應(yīng)用場(chǎng)景簡(jiǎn)述
- jQuery.fn和jQuery.prototype區(qū)別介紹
- Javascript中Array.prototype.map()詳解
- JavaScript中的prototype使用說(shuō)明
- 不錯(cuò)的一篇關(guān)于javascript-prototype繼承
- 找到了一篇jQuery與Prototype并存的沖突的解決方法
相關(guān)文章
全面了解javascript中的錯(cuò)誤處理機(jī)制
下面小編就為大家?guī)?lái)一篇全面了解javascript中的錯(cuò)誤處理機(jī)制。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07JavaScript導(dǎo)航腳本判斷當(dāng)前導(dǎo)航
這篇文章主要介紹了JavaScript導(dǎo)航腳本判斷當(dāng)前導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07深入理解 TypeScript Reflect Metadata
這篇文章主要介紹了深入理解 TypeScript Reflect Metadata,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12常用的Javascript設(shè)計(jì)模式小結(jié)
javascript設(shè)計(jì)模式有很多種,本文給大家介紹常用的javascript設(shè)計(jì)模式,對(duì)javascript設(shè)計(jì)模式相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12js正文內(nèi)容高亮效果的實(shí)現(xiàn)方法
這篇文章介紹了js正文內(nèi)容高亮效果的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-06-06JavaScript實(shí)現(xiàn)二叉樹(shù)的先序、中序及后序遍歷方法詳解
這篇文章主要介紹了JavaScript實(shí)現(xiàn)二叉樹(shù)的先序、中序及后序遍歷方法,結(jié)合實(shí)例形式總結(jié)分析了javascript二叉樹(shù)的先序、中序及后序遍歷實(shí)現(xiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2017-10-10微信小程序用戶授權(quán)、位置授權(quán)及獲取微信綁定手機(jī)號(hào)
這篇文章主要介紹了信小程序用戶授權(quán)、位置授權(quán)及獲取微信綁定手機(jī)號(hào),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07