在设置构造函数时,是否可以将其参数直接传递到其方法之一?这就是我的意思:
function jedi(name,text){
this.name = name;
this.quote = function quote(name,text){
return name + " said " + text;
};
}
var obiwan = new jedi('Obi-Wan','Fear leads to the darkside');
console.log(obiwan.quote()); //renders as undefined said undefined
//this works fine though
console.log(obiwan.quote('Obi-Wan','Fear leads to the darkside'));
是否可以将“name”和“text”参数直接从“var obiwan = new jedi()”传递到“obiwan.quote()”? 我希望我的问题有意义。预先感谢任何可以帮助我的人!
最佳答案
只使用实例变量?
function jedi(name,text){
this.name = name;
this.text = text;
this.quote = function quote(){
return this.name + " said " + this.text;
};
}
var obiwan = new jedi('Obi-Wan','Fear leads to the darkside');
console.log(obiwan.quote()); //works like a charm
关于javascript - 将 javascript 参数从对象构造函数传递到对象的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23878318/