Javascript 继承原型JS时如何传递参数?
例如,我们有一个函数Javascript 继承原型JS时如何传递参数?,javascript,parameter-passing,prototype,prototypal-inheritance,Javascript,Parameter Passing,Prototype,Prototypal Inheritance,例如,我们有一个函数 function FirstFunction(name, surname){ this.name = name; this.surname = surname; ... } 我们在它的原型中有一些函数,在它自己的原型中有另一个函数SecondFunction。当我想继承原型时,我会写 SecondFunction.prototype = Object.create(FirstFunction.prototype); 现在,当我尝试使用 var n
function FirstFunction(name, surname){
this.name = name;
this.surname = surname;
...
}
我们在它的原型中有一些函数,在它自己的原型中有另一个函数SecondFunction。当我想继承原型时,我会写
SecondFunction.prototype = Object.create(FirstFunction.prototype);
现在,当我尝试使用
var newVariable = new SecondFunction();
我想传递FirstFunction中列出的参数'name'和'姓氏',以便能够在FirstFunction的原型中使用函数。哪种方法最好?我认为正确的方法是使用或: 函数名{ this.name=名称; this.姓氏=姓氏; } 函数名称,姓氏{ FirstFunction.callthis,name,姓氏 } SecondFunction.prototype=Object.createFirstFunction.prototype; var newVariable=newsecondfunction'Harry','Potter';
console.lognewVariable;An.applythis,参数将传递任意数量的变量,因此,即使FirstFunction现在接受第三个名为age的变量,您也不需要更改SecondFunction以适应它。@VLAZ如果父构造函数更改其接口,您仍然需要更改子类