2009-10-10 41 views
0

作为以下代码,我的问题是:
in testFunction,我怎样才能调用函数删除?
在删除我如何可以调用testFunction?
在删除,我怎么能打电话添加?
在JavaScript对象字面量中,如何引用具有相同级别或更高级别的函数?

十分感谢

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;  
     } 

    } 
} 

};

回答

2

testFunction()访问remove()你可以这样做:

this.classValue.remove(); 

为了走另外一条路,我认为你必须使用

stringhelper.testFunction(); 

因为你没有包含一个变量函数或父项this对象(您可以在关闭中使用它)。

+0

为什么不'stringhelper.classValue.remove()'?这样,函数的别名不会混淆“this”引用。 – kangax 2009-10-11 07:19:09

相关问题