我想将选择器值发送到原型(prototype)。目前我正在使用
var selector; //id of html element
function $(selector)
{
if(window===this)
return new $(selector);
return this;
}
$.prototype={
tooltip: function(){
console.log(selector);
//do calculations with the selector value
return this;
}
};
显示未定义。
有没有办法传递变量?
最佳答案
不,您无法从原型(prototype)方法访问它。 selector
参数是构造函数的局部变量。
但是,您可以将其作为实例的属性提供:
function $(selector) {
if(!(this instanceof $))
return new $(selector);
this.selector = selector; // assigns the variable to a property
}
$.prototype.tooltip = function(){
console.log(this.selector); // access the property on the instance
//do calculations with the selector value
return this;
};
关于javascript - 将变量传递给原型(prototype) Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17027093/