Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active August 25, 2018 22:19
Show Gist options
  • Select an option

  • Save externvoid/3736035676802374daf3fadbc1699289 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/3736035676802374daf3fadbc1699289 to your computer and use it in GitHub Desktop.
メソッド探索チェーン、プロトタイプチェーン
// コンストラクタ・エリア、プロトタイプ・エリア
// クラス・メソッドが配置されている、インスタンス・メソッドが配置されている
// メソッド探索チェーンとは?

// Arrayオブジェクトの場合とArrayのインスタンスの場合
                                                                                
ar = []
ar.push('OK')
// インスタンス・メソッドpushの呼出
console.log(ar) // => [ 'OK' ]

// インスタンス・メソッドの追加
 Array.prototype.myMethod = function() {
  console.log('Oops')
}
ar.myMethod() // => Oops

console.log(Array.isArray(ar)) // => true
// コンストラクタ・メソッドの追加
Array.constructor.prototype.myMethod2 = function() {
  console.log('Awesome')
}
// ar.myMethod2() // => TypeError: ar.myMethod2 is not a function
Array.myMethod2 // => Awesome

console.log(ar) // => [ 'OK' ]
console.log(Array) // => [Function: Array], Constructor Function Array
// console.log(ar.prototype.myMethod)
console.log(Array.prototype) // => [ myMethod: [Function] ]

console.log(Array.constructor) // => [Function: Function]
console.log(Array.constructor.prototype) // => { [Function] myMethod2: [Function
] }
console.log(Array.prototype[Symbol.unscopables])
// { copyWithin: true,
//   entries: true,
//   fill: true,
//   find: true,
//   findIndex: true,
//   includes: true,
//   keys: true }


console.log(Array.__proto__) // same as Array.constructor.prototype
console.log(ar.__proto__) // same as Array.prototype
  
  
// see also. 'Standard Built-in Object
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array
// 図で理解するJavaScriptのプロトタイプチェーン 
// https://qiita.com/howdy39/items/35729490b024ca295d6c 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment