【发布时间】:2016-11-05 07:11:26
【问题描述】:
我了解如何编写正确的继承,但我试图准确了解我在编写此代码时所做的事情,以便完全理解我的代码。我已经阅读了一些 MDN,但我并不完全理解它,非常感谢您的帮助,谢谢我有点困惑
我知道我应该这样写继承
function Employee(){
this.pay=' ';
this.dept=' ';
}
function Manager(){
Employee.call(this);
this.reports=[];
}
Manager.prototype=Object.create(Employee.prototype);
据我了解,function.call 的工作方式与此类似,所以当你 this 作为 arg 它会更改为 arg1 指向的位置,例如,它基本上允许您更改为 'this',指向
function Employee(context,arg1,arg2,arg3){
context.arg1=arg1;
context.arg2=arg2;
context.arg3=arg3;
}
所以 function.call 不会创建继承,因为它只是将属性复制到新的函数和对象,并且不会强制它沿着原型链向上走,所以你在重复你自己,根据我的理解,它是唯一的使用 will to 从另一个函数中复制参数。
如果我只是将 Function.call 与构造函数一起使用,则此准继承有效,也就是说,如果我只是将值复制到一个新对象中,就像这样
function Employee(){
this.pay=' ';
this.dept=' ';
}
function Manager(){
Employee.call(this);
this.reports=[];
}
//this works!!
/*but all the objects created with Manager() will have all the properties from Employee copied directly into it, and it doesnt walk the prototype chain to find them*/
据我了解,Object.create(function.prototype) 创建了一个新对象,因此它不引用原始函数原型并创建继承,这可以像我期望的那样工作并创建继承,它沿着原型链向上走并且没有t 将属性复制到新对象中
var animal={
moves: true
}
var snake = Object.create(animal);
snake.noLeggs=true;
snake.coldBlooded= true;
var python= Object.create(snake);
python.size='large';
//if I were to type python.moves it would return true
//but python doesn't have a property named pay so its walking up the prototype chain to find it
但是这似乎不适用于构造函数,所以我对我做错了什么或 function.prototype=Object.create(super Function.prototype); 的目的有点困惑那就是您似乎无法使用我的经验继承来创建原型链,例如以下内容对我不起作用
function Employee(){
this.pay=' ';
this.depth=' ';
}
function Manager(){
this.reports=[];
}
Manager.prototype=Object.create(Employee.prototype);
//however if I were to type var tom= new Manager(); tom.pay, pay would return "undefined"
//so it appears I need Function.call()
所以在我看来这没有做任何事情,所以我错过了什么,我做错了什么,如果我没有做错任何事情,目的是什么,我不明白什么?我可以让它工作的唯一方法是使用 Function.call 并且无论有没有 function.prototype=Object.create(super function.prototype); 似乎都以相同的方式工作,但最好是它走起来一个原型链,这样代码就干了。
【问题讨论】:
标签: javascript performance prototype