js & object & prototype & __proto__ & prototype chain
js & object & prototype & proto & prototype chain
constructor prototype === instance proto
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype
let log = console.log;
function Foo() {
name: "Foo";
}
// var foo = new Foo();
let foo = new Foo();
log(Foo.name);
log(foo.name);
log(`Foo.prototype =`, Foo.prototype);
log(`foo.prototype =`, foo.prototype);
log(`Foo.__proto__ =`, Foo.__proto__);
log(`foo.__proto__ =`, foo.__proto__);
log(`Foo.prototype === foo.__proto__`, Foo.prototype === foo.__proto__);
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-16
*
* @description prototype-chain (prototype & __proto__ )
* @augments
* @example
* @link https://hackernoon.com/understand-nodejs-javascript-object-inheritance-proto-prototype-class-9bd951700b29
*
*/
let log = console.log;
function Foo() {
name: "Foo";
}
// var foo = new Foo();
let foo = new Foo();
log(Foo.name);
// Foo
log(foo.name);
// undefined
log(`Foo.prototype =`, Foo.prototype);
// {constructor: ƒ}
// constructor: ƒ Foo()
// __proto__: Object
log(`foo.prototype =`, foo.prototype);
// undefined
log(`Foo.__proto__ =`, Foo.__proto__);
// ƒ () { [native code] }
log(`foo.__proto__ =`, foo.__proto__);
// {constructor: ƒ}
// constructor: ƒ Foo()
// __proto__: Object
log(`Foo.prototype === foo.__proto__`, Foo.prototype === foo.__proto__);
// true
https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript
https://www.freecodecamp.org/news/prototype-in-js-busted-5547ec68872/
( new Foo ).__proto__ === Foo.prototype;
( new Foo ).prototype === undefined;
function Point(x, y) {
this.x = x;
this.y = y;
}
var myPoint = new Point();
// the following are all true
myPoint.__proto__ == Point.prototype
myPoint.__proto__.__proto__ == Object.prototype
myPoint instanceof Point;
myPoint instanceof Object;
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11363819.html
未经授权禁止转载,违者必究!