Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 继承原型JS时如何传递参数?_Javascript_Parameter Passing_Prototype_Prototypal Inheritance - Fatal编程技术网

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如果父构造函数更改其接口,您仍然需要更改子类