小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JS基礎(chǔ)知識(shí)理解之一: 原型鏈

 頭號(hào)碼甲 2021-10-27

js原型鏈

js原型鏈?zhǔn)鞘裁矗?/p>

在思考這個(gè)問題的時(shí)候,我們可能會(huì)有很多概念,【鏈子】、【祖先】、【father】

  1. 要理解 原型鏈 首先要理解 原型

    對(duì)象的屬性 [[Prototype]] 都指向其他對(duì)象,Object.prototype 的 [[Prototype]] 例外。

  2. 單純從 原型鏈 這個(gè)這個(gè)詞來理解,js原型鏈更像是一種copy 或 引用。

    簡單理解就是原型組成的鏈,js引擎會(huì)通過 __proto__ 一層層的往上鏈接。這條鏈就是原型鏈

  1. 下面通過代碼來演示一下原型鏈
function People() {
}

People.prototype.sayHello = function() {
  console.log('hello, guy!');
}

var xiaoMing = new People();

console.log(xiaoMing.sayHello()) // hello, guy!
xiaoMing.__proto__ === Person.prototype;
Object.getPrototypeOf(xiaoMing) === Person.prototype;

Object.getPrototypeOf(People) // ? () { [native code] }
Object.getPrototypeOf(People) === Function.prototype // true;
Object.getPrototypeOf(People.prototype) 
// {constructor: ?, __defineGetter__: ?, __defineSetter__: ?, hasOwnProperty: ?, __lookupGetter__: ?, …}

從代碼來跟蹤:

  • xiaoMing 的__proto__指向 People.prototype
  • People.prototype 的 __proto__ 指向Object.Prototype
  1. 下面進(jìn)入更加模糊的關(guān)系。。
Object.getPrototypeOf(Object)
// ? () { [native code] }

Object.getPrototypeOf(Function)
// ? () { [native code] }

Function.__proto__
// ? () { [native code] }

Object.__proto__
// ? () { [native code] }

Object.getPrototypeOf(Function.prototype)
// {constructor: ?, __defineGetter__: ?, __defineSetter__: ?, hasOwnProperty: ?, __lookupGetter__: ?, …}

Object.getPrototypeOf(Object.prototype)
// null

Object.getPrototypeOf(100)
// Number {0, constructor: ?, toExponential: ?, toFixed: ?, toPrecision: ?, …}
constructor: ? Number()
toExponential: ? toExponential()
toFixed: ? toFixed()
toLocaleString: ? toLocaleString()
toPrecision: ? toPrecision()
toString: ? toString()
valueOf: ? valueOf()
__proto__: Object
[[PrimitiveValue]]: 0

// 下面看幾種數(shù)據(jù)類型的prototype 都是?

100 instanceof Number
// false
100 instanceof Object
// false


Object.getPrototypeOf(obj)
//{constructor: ?, __defineGetter__: ?, __defineSetter__: ?, hasOwnProperty: ?, __lookupGetter__: ?, …}
obj instanceof Object
//true

Object.getPrototypeOf('test')
//String {"", constructor: ?, anchor: ?, big: ?, blink: ?, …}anchor: ? anchor()big: ? big()blink: ? blink()bold: ? bold()charAt: ? charAt()charCodeAt: ? charCodeAt()codePointAt: ? codePointAt()concat: ? concat()constructor: ? String()endsWith: ? endsWith()fixed: ? fixed()fontcolor: ? fontcolor()fontsize: ? fontsize()includes: ? includes()indexOf: ? indexOf()italics: ? italics()lastIndexOf: ? lastIndexOf()length: 0link: ? link()localeCompare: ? localeCompare()match: ? match()matchAll: ? matchAll()normalize: ? normalize()padEnd: ? padEnd()padStart: ? padStart()repeat: ? repeat()replace: ? replace()search: ? search()slice: ? slice()small: ? small()split: ? split()startsWith: ? startsWith()strike: ? strike()sub: ? sub()substr: ? substr()substring: ? substring()sup: ? sup()toLocaleLowerCase: ? toLocaleLowerCase()toLocaleUpperCase: ? toLocaleUpperCase()toLowerCase: ? toLowerCase()toString: ? toString()toUpperCase: ? toUpperCase()trim: ? trim()trimEnd: ? trimEnd()trimLeft: ? trimStart()trimRight: ? trimEnd()trimStart: ? trimStart()valueOf: ? valueOf()Symbol(Symbol.iterator): ? [Symbol.iterator]()__proto__: Object[[PrimitiveValue]]: ""
'test' instanceof String
//false
String('test') === 'test'
//true
String('test') instanceof String
// false


  • Object 是有Function 創(chuàng)建的

  • Function.prototype 的 原型是 Object.Prototype

  • Function.prototype 是 Function。

  • 函數(shù)是有它自己創(chuàng)建的,難道是為了保持一致??

下面這張圖,展示更加錯(cuò)綜復(fù)雜的原型鏈。

是不是很迷惑。

Object 是有Function 創(chuàng)建的,這個(gè)正好符合寫普通fuction的寫法,function Object() {}

只不過這次函數(shù)名是 Object而已。

看下面如果我們把Object 重寫了。這樣只要通過Object 構(gòu)造函數(shù)創(chuàng)建的原型都已經(jīng)被改寫

function Object() {}
// undefined

var a = new Object();
// undefined

a.__proto__
// {constructor: ?}


var c = new Object({})
c.__proto__
// {constructor: ?}

var d = new Object({name: 'dd'})
d.__proto__
// {constructor: ?}

Object.__proto__
// ? () { [native code] }

最后又回到了 Function

js 就是函數(shù)式編程,一切皆函數(shù),哈哈。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多