我有这个代码:
function Q(a){
function cElems(e,f,p){var l=e.length,i=0;if(e instanceof Array){while(i<l){f(e[i],p);i++}}else{f(e,p)}}
if(typeof a=="string"){
var b=a[0],c=a.substr(1),r=[].slice.call(document.getElementsByClassName(c));
return{
setClass:function(b){cElems(r,function(e,p){e.className=p},b)}
};
}
}
我想检查是否调用了包装函数,即:Q(".test").setClass("test2")
,如果没有,则返回不同的内容,如下所示:
if(wrapped==true){
return{
setClass:function(b){cElems(r,function(e,p){e.className=p},b)}
};
}else{
return "no constructor was called";
}
这可能吗?
最佳答案
在Q(..).x()
中,Q(..)
总是在x<之前调用
正在解析(并调用);通过重写可以清楚地看到这一点:
var y = Q(..); // this executes and the value is assigned to y
y.x(); // and then the method is invoked upon the object named by y
因此,不可能根据调用 Q(..).x
的结果来更改 Q(..)
已执行的行为 -对象已从 Q(..)
返回。
关于javascript - 检查包装函数是否被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028993/