在javascript对象文本中,如何引用具有相同级别或更高级别的函数?
作为以下代码,我的问题是:在javascript对象文本中,如何引用具有相同级别或更高级别的函数?,javascript,json,object,Javascript,Json,Object,作为以下代码,我的问题是: 在testFunction中,如何调用函数remove? 在remove中如何调用testFunction? 在删除中,如何调用添加? 非常感谢 var stringhelper={ testFunction:function(){}, //deal with text such as: //"alignleft red bold header2" classValue:{ //test whether classValue has className in
在testFunction中,如何调用函数remove?
在remove中如何调用testFunction?
在删除中,如何调用添加?
非常感谢
var stringhelper={
testFunction:function(){},
//deal with text such as:
//"alignleft red bold header2"
classValue:{
//test whether classValue has className in it
has:function(classValue,className){
var regex=new RegExp("(^|\\s+)"+className+"(\\s+|$)");
return regex.test(classValue);
},
remove:function(classValue,className){
return classValue.replace(className,"").replace(/\s+/g," ").replace(/^\s+|\s+$/g,"");
},
add:function(classValue,className){
if(/^\s+$/.test(classValue)){
return className;
}
if(!this.has(classValue,className)){
return classValue+" "+className;
}
}
}
}) 要从testFunction()
访问remove()
,您可以执行以下操作:
this.classValue.remove();
换个方向我想你得用
stringhelper.testFunction();
因为您没有包含函数或父对象的变量
此
对象(可以在闭包中使用)。为什么不stringhelper.classValue.remove()
?这样,函数的别名就不会打乱这个引用。