function Parent(name){ name = "Parent" }// 给parent的原型加上一个方法Parent.prototype.parentMethod = function parentMethod(){};function Child(name ){ name = "child" }Child.prototype = Object.create(Parent.prototype);console.log("constructor:"+ Child.constructor +"prototype constructor:"+Child.prototype.constructor)Child.prototype.constructor = Child ;//returnconsole.log("显示指定自身构造函数之后:\n"+"constructor:"+ Child.constructor +"prototype constructor:"+Child.prototype.constructor)let
打印结果:
在显示指定自身构造函数之后Child.constructor 没有改变
而Child.prototype.constructor 从parent变成child的
chenyanlongdeMacBook-Pro-3:Object chenyanlong$ node inherit_constructor.jsconstructor:function Function() { [native code] }prototype constructor:function Parent(name){ name = "Parent" }显示指定自身构造函数之后:constructor:function Function() { [native code] }prototype constructor:function Child(name ){ name = "child" }
2.显示指定原型构造函数为child之后,可以继续使用parent原型有的方法
代码
function Parent(name){ name = "Parent" }// 给parent的原型加上一个方法Parent.prototype.parentMethod = function parentMethod(){console.log("I am parent method")};function Child(name ){ name = "child" }Child.prototype = Object.create(Parent.prototype);console.log("constructor:"+ Child.constructor +"prototype constructor:"+Child.prototype.constructor)Child.prototype.constructor = Child ;//returnconsole.log("显示指定自身构造函数之后:\n"+"constructor:"+ Child.constructor +"prototype constructor:"+Child.prototype.constructor)let child = new Child()child.parentMethod()chenyanlongdeMacBook-P
结果:
child调用parentMethod —> 成功执行了parent的parentMethod
constructor:function Function() { [native code] }prototype constructor:function Parent(name){ name = "Parent" }显示指定自身构造函数之后:constructor:function Function() { [native code] }prototype constructor:function Child(name ){ name = "child" }I am parent method
3.child可以重写parent.prototype的方法
function Parent(name){ name = "Parent" }// 给parent的原型加上一个方法Parent.prototype.parentMethod = function parentMethod(){console.log("I am parent method")};function Child(name ){ name = "child" }Child.prototype = Object.create(Parent.prototype);console.log("constructor:"+ Child.constructor +"prototype constructor:"+Child.prototype.constructor)Child.prototype.parentMethod = function childMethod(){console.log("i am child ,I overwrite the parentMethod")}Child.prototype.constructor = Child ;//returnconsole.log("显示指定自身构造函数之后:\n"+"constructor:"+ Child.constructor +"prototype constructor:"+Child.prototype.constructor)let child = new Child()child.parentMethod()
打印结果:
constructor:function Function() { [native code] }prototype constructor:function Parent(name){ name = "Parent" }显示指定自身构造函数之后:constructor:function Function() { [native code] }prototype constructor:function Child(name ){ name = "child" }i am child ,I overwrite the parentMethod
4. prototype.constructor = child的好处
代码1:
function Parent() {};function CreatedConstructor() {}CreatedConstructor.prototype = Object.create(Parent.prototype);CreatedConstructor.prototype.create = function create() {return new this.constructor();}new CreatedConstructor().create().create();// error undefined is not a function since constructor === Parent//这里报错的原因是此时CreatedConstructor.prototype.constructor = function Parent() {};// 并没有create方法
代码2:
function Parent() {};function CreatedConstructor() {}CreatedConstructor.prototype = Object.create(Parent.prototype);CreatedConstructor.prototype.constructor = CreatedConstructor; // set right constructor for further usingCreatedConstructor.prototype.create = function create() {return new this.constructor();}//这里不会报错的原因是://CreatedConstructor.prototype.constructor = CreatedConstructor//有自己的create方法new CreatedConstructor().create().create(); // it's pretty fine
4.1 prototype.constructor = child 时,prototype.constructor 可以调用parent原型的方法吗
结果:可以显示调用
function Parent() {};Parent.prototype.ParentMethod = function h(){console.log("i am parent.prototype method")}function CreatedConstructor() {}CreatedConstructor.prototype = Object.create(Parent.prototype);CreatedConstructor.prototype.constructor = CreatedConstructor; // set right constructor for further usingCreatedConstructor.prototype.create = function create() {return new this.constructor();}CreatedConstructor.prototype.childMethod = function (){this.constructor.prototype.ParentMethod();}new CreatedConstructor().create().create().childMethod(); // it's pretty fine
5. 在child.prototype.method 中调用parent.prototype.method的方法
function ParentWithStatic() {}ParentWithStatic.prototype.startPosition = { x: 0, y:0 };ParentWithStatic.prototype.getStartPosition = function getStartPosition() {return this.startPosition;}function Child(x, y) {this.position = {x: x,y: y};}Child.prototype = Object.create(ParentWithStatic.prototype)Child.prototype.constructor = Child;Child.prototype.constructor = ParentWithStatic;Child.prototype.getOffsetByInitialPosition = function getOffsetByInitialPosition() {var position = this.position;var startPosition = this.constructor.prototype.getStartPosition(); // error undefined is not a function, since the constructor is Childreturn {offsetX: startPosition.x - position.x,offsetY: startPosition.y - position.y}};child = new Child();
