ObjMaker = function() {this.a = 'first';};// ObjMaker is just a function, there's nothing special about it that makes// it a constructor.
ObjMaker.prototype.b = 'second';// like all functions, ObjMaker has an accessible prototype property that// we can alter. I just added a property called 'b' to it. Like// all objects, ObjMaker also has an inaccessible [[prototype]] property// that we can't do anything with
obj1 = new ObjMaker();// 3 things just happened.// A new, empty object was created called obj1. At first obj1 was the same// as {}. The [[prototype]] property of obj1 was then set to the current// object value of the ObjMaker.prototype (if ObjMaker.prototype is later// assigned a new object value, obj1's [[prototype]] will not change, but you// can alter the properties of ObjMaker.prototype to add to both the// prototype and [[prototype]]). The ObjMaker function was executed, with// obj1 in place of this... so obj1.a was set to 'first'.
obj1.a;// returns 'first'obj1.b;// obj1 doesn't have a property called 'b', so JavaScript checks// its [[prototype]]. Its [[prototype]] is the same as ObjMaker.prototype// ObjMaker.prototype has a property called 'b' with value 'second'// returns 'second'
这就像类继承,因为现在,您使用new ObjMaker()创建的任何对象似乎也继承了'b'属性。
如果你想要一个子类,那么你这样做:
SubObjMaker = function () {};SubObjMaker.prototype = new ObjMaker(); // note: this pattern is deprecated!// Because we used 'new', the [[prototype]] property of SubObjMaker.prototype// is now set to the object value of ObjMaker.prototype.// The modern way to do this is with Object.create(), which was added in ECMAScript 5:// SubObjMaker.prototype = Object.create(ObjMaker.prototype);
SubObjMaker.prototype.c = 'third';obj2 = new SubObjMaker();// [[prototype]] property of obj2 is now set to SubObjMaker.prototype// Remember that the [[prototype]] property of SubObjMaker.prototype// is ObjMaker.prototype. So now obj2 has a prototype chain!// obj2 ---> SubObjMaker.prototype ---> ObjMaker.prototype
obj2.c;// returns 'third', from SubObjMaker.prototype
obj2.b;// returns 'second', from ObjMaker.prototype
obj2.a;// returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype// was created with the ObjMaker function, which assigned a for us
var func1 = function (x) { this.x = x; } // Used with 'new' onlyvar func2 = function (x) { var z={}; z.x = x; return z; } // Used both waysfunc1.prototype.y = 11;func2.prototype.y = 12;
A1 = new func1(1); // Has A1.x AND A1.yA2 = func1(1); // Undefined ('this' refers to 'window')B1 = new func2(2); // Has B1.x ONLYB2 = func2(2); // Has B2.x ONLY
function Foo() {return this;}
var a = Foo(); // Returns the 'window' objectvar b = new Foo(); // Returns an empty object of foo
a instanceof Window; // Truea instanceof Foo; // False
b instanceof Window; // Falseb instanceof Foo; // True
var Foo = function(){this.A = 1;this.B = 2;};var bar = new Foo();console.log(bar()); //illegal isn't pointing to a function but an objectconsole.log(bar.A); //prints 1
var Foo = function(){this.A = 1;this.B = 2;return {C:20,D:30};};var bar = new Foo();console.log(bar.C);//prints 20console.log(bar.A); //prints undefined. bar is not pointing to the object which got created due to new keyword.
" Every object (including functions) has this internal property called [[prototype]]"
每个函数都有一个原型。类型对象,自动设置为使用该函数创建的对象的原型。
您可以轻松查看:
const a = { name: "something" };console.log(a.prototype); // undefined because it is not directly accessible
const b = function () {console.log("somethign");};
console.log(b.prototype); // returns b {}
{// other propertiesprototype: {// shared space which automatically gets [[prototype]] linkagewhen "new" keyword is used on creating instance of "ConstructorFunction"}}