1. function Parent(name){ name = "Parent" }
  2. // parent的原型加上一个方法
  3. Parent.prototype.parentMethod = function parentMethod(){};
  4. function Child(name ){ name = "child" }
  5. Child.prototype = Object.create(Parent.prototype);
  6. console.log(
  7. "constructor:"+ Child.constructor +
  8. "prototype constructor:"+Child.prototype.constructor
  9. )
  10. Child.prototype.constructor = Child ;//return
  11. console.log(
  12. "显示指定自身构造函数之后:\n"+
  13. "constructor:"+ Child.constructor +
  14. "prototype constructor:"+Child.prototype.constructor
  15. )
  16. let

打印结果:
在显示指定自身构造函数之后Child.constructor 没有改变
而Child.prototype.constructor 从parent变成child的

  1. chenyanlongdeMacBook-Pro-3:Object chenyanlong$ node inherit_constructor.js
  2. constructor:function Function() { [native code] }prototype constructor:function Parent(name){ name = "Parent" }
  3. 显示指定自身构造函数之后:
  4. constructor:function Function() { [native code] }prototype constructor:function Child(name ){ name = "child" }

2.显示指定原型构造函数为child之后,可以继续使用parent原型有的方法

代码

  1. function Parent(name){ name = "Parent" }
  2. // parent的原型加上一个方法
  3. Parent.prototype.parentMethod = function parentMethod(){
  4. console.log("I am parent method")
  5. };
  6. function Child(name ){ name = "child" }
  7. Child.prototype = Object.create(Parent.prototype);
  8. console.log(
  9. "constructor:"+ Child.constructor +
  10. "prototype constructor:"+Child.prototype.constructor
  11. )
  12. Child.prototype.constructor = Child ;//return
  13. console.log(
  14. "显示指定自身构造函数之后:\n"+
  15. "constructor:"+ Child.constructor +
  16. "prototype constructor:"+Child.prototype.constructor
  17. )
  18. let child = new Child()
  19. child.parentMethod()
  20. chenyanlongdeMacBook-P

结果:
child调用parentMethod —> 成功执行了parent的parentMethod

  1. constructor:function Function() { [native code] }prototype constructor:function Parent(name){ name = "Parent" }
  2. 显示指定自身构造函数之后:
  3. constructor:function Function() { [native code] }prototype constructor:function Child(name ){ name = "child" }
  4. I am parent method

3.child可以重写parent.prototype的方法

  1. function Parent(name){ name = "Parent" }
  2. // parent的原型加上一个方法
  3. Parent.prototype.parentMethod = function parentMethod(){
  4. console.log("I am parent method")
  5. };
  6. function Child(name ){ name = "child" }
  7. Child.prototype = Object.create(Parent.prototype);
  8. console.log(
  9. "constructor:"+ Child.constructor +
  10. "prototype constructor:"+Child.prototype.constructor
  11. )
  12. Child.prototype.parentMethod = function childMethod(){
  13. console.log("i am child ,I overwrite the parentMethod")
  14. }
  15. Child.prototype.constructor = Child ;//return
  16. console.log(
  17. "显示指定自身构造函数之后:\n"+
  18. "constructor:"+ Child.constructor +
  19. "prototype constructor:"+Child.prototype.constructor
  20. )
  21. let child = new Child()
  22. child.parentMethod()

打印结果:

  1. constructor:function Function() { [native code] }prototype constructor:function Parent(name){ name = "Parent" }
  2. 显示指定自身构造函数之后:
  3. constructor:function Function() { [native code] }prototype constructor:function Child(name ){ name = "child" }
  4. i am child ,I overwrite the parentMethod

4. prototype.constructor = child的好处

代码1:

  1. function Parent() {};
  2. function CreatedConstructor() {}
  3. CreatedConstructor.prototype = Object.create(Parent.prototype);
  4. CreatedConstructor.prototype.create = function create() {
  5. return new this.constructor();
  6. }
  7. new CreatedConstructor().create().create();
  8. // error undefined is not a function since constructor === Parent
  9. //这里报错的原因是此时CreatedConstructor.prototype.constructor = function Parent() {};
  10. // 并没有create方法

代码2:

  1. function Parent() {};
  2. function CreatedConstructor() {}
  3. CreatedConstructor.prototype = Object.create(Parent.prototype);
  4. CreatedConstructor.prototype.constructor = CreatedConstructor; // set right constructor for further using
  5. CreatedConstructor.prototype.create = function create() {
  6. return new this.constructor();
  7. }
  8. //这里不会报错的原因是:
  9. //CreatedConstructor.prototype.constructor = CreatedConstructor
  10. //有自己的create方法
  11. new CreatedConstructor().create().create(); // it's pretty fine

4.1 prototype.constructor = child 时,prototype.constructor 可以调用parent原型的方法吗

结果:可以显示调用

  1. function Parent() {};
  2. Parent.prototype.ParentMethod = function h(){console.log("i am parent.prototype method")}
  3. function CreatedConstructor() {}
  4. CreatedConstructor.prototype = Object.create(Parent.prototype);
  5. CreatedConstructor.prototype.constructor = CreatedConstructor; // set right constructor for further using
  6. CreatedConstructor.prototype.create = function create() {
  7. return new this.constructor();
  8. }
  9. CreatedConstructor.prototype.childMethod = function (){
  10. this.constructor.prototype.ParentMethod();
  11. }
  12. new CreatedConstructor().create().create().childMethod(); // it's pretty fine

5. 在child.prototype.method 中调用parent.prototype.method的方法

  1. function ParentWithStatic() {}
  2. ParentWithStatic.prototype.startPosition = { x: 0, y:0 };
  3. ParentWithStatic.prototype.getStartPosition = function getStartPosition() {
  4. return this.startPosition;
  5. }
  6. function Child(x, y) {
  7. this.position = {
  8. x: x,
  9. y: y
  10. };
  11. }
  12. Child.prototype = Object.create(ParentWithStatic.prototype)
  13. Child.prototype.constructor = Child;
  14. Child.prototype.constructor = ParentWithStatic;
  15. Child.prototype.getOffsetByInitialPosition = function getOffsetByInitialPosition() {
  16. var position = this.position;
  17. var startPosition = this.constructor.prototype.getStartPosition(); // error undefined is not a function, since the constructor is Child
  18. return {
  19. offsetX: startPosition.x - position.x,
  20. offsetY: startPosition.y - position.y
  21. }
  22. };
  23. child = new Child();