2009-10-10 44 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

相關問題