免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
詳解prototype與

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__是每個對象都有的一個屬性,而prototype是函數(shù)才會有的屬性!!!
使用Object.getPrototypeOf()代替__proto__!!!

一、prototype

幾乎所有的函數(shù)(除了一些內(nèi)建函數(shù))都有一個名為prototype(原型)的屬性,這個屬性是一個指針,指向一個對象,而這個對象的用途是包含可以有特定類型的所有實例共享的屬性和方法。prototype是通過調(diào)用構(gòu)造函數(shù)而創(chuàng)建的那個對象實例的原型對象。hasOwnProperty()判斷指定屬性是否為自有屬性;in操作符對原型屬性和自有屬性都返回true。
示例:自有屬性&原型屬性

var obj = {a: 1};obj.hasOwnProperty("a"); // trueobj.hasOwnProperty("toString"); // false"a" in obj; // true"toString" in obj; // true
  • 1
  • 2
  • 3
  • 4
  • 5

示例:鑒別原型屬性

function hasPrototypeProperty(obj, name){    return name in obj && !obj.hasOwnProperty(name);}
  • 1
  • 2
  • 3

二、__proto__

對象具有屬性__proto__,可稱為隱式原型,一個對象的隱式原型指向構(gòu)造該對象的構(gòu)造函數(shù)的原型,這也保證了實例能夠訪問在構(gòu)造函數(shù)原型中定義的屬性和方法。

function Foo(){}var Boo = {name: "Boo"};Foo.prototype = Boo;var f = new Foo();console.log(f.__proto__ === Foo.prototype); // trueconsole.log(f.__proto__ === Boo);   // trueObject.getPrototypeOf(f) === f.__proto__;   // true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三、Object.getPrototypeOf()

一個對象實例通過內(nèi)部屬性[[Prototype]]跟蹤其原型對象。使用原型對象的好處是可以讓所有對象實例共享它所包含的屬性和方法??梢哉{(diào)用對象的Object.getPrototypeOf()方法讀取[[Prototype]]屬性的值,也可以使用isPrototypeOf()方法檢查某個對象是否是另一個對象的原型對象。大部分JavaScript引擎在所有對象上都支持一個名為__proto__的屬性,該屬性可以直接讀寫[[Prototype]]屬性。
示例:原型對象

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 ligangp2.sayName();   // my name is Camile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
什么是原型鏈
JavaScript中的原型和繼承
深入理解javascript原型和閉包(4)
javascript——prototype與
前端面試送命題-JS三座大山
js原型與原型鏈的詳細理解
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服