将javascript参数从对象构造函数传递到对象';什么方法?
在设置构造函数时,是否有可能将其参数直接传递到其中一个方法中?我的意思是:将javascript参数从对象构造函数传递到对象';什么方法?,javascript,object,methods,parameter-passing,Javascript,Object,Methods,Parameter Passing,在设置构造函数时,是否有可能将其参数直接传递到其中一个方法中?我的意思是: 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'); conso
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
为
quote
函数的参数指定与构造函数参数不同的名称
function jedi(name,text){
this.name = name;
this.quote = function quote(_name,_text){
return (_name || name) + " said " + (_text || text);
};
}
var obiwan = new jedi('Obi-Wan','Fear leads to the darkside');
console.log(obiwan.quote());
// would rendera as Obi-Wan said Fear leads to the darkside
console.log(obiwan.quote('I', "is the new X-men inspired from Assassin's Creed?"));
// would rendera as I said is the new X-men inspired from Assassin's Creed?
请仔细阅读此:天哪,谢谢。我知道我在眼前错过了一些愚蠢的事情!谢谢你,贾里