开发者

in a javascript object literal, how to reference a function that has the same level or upper level?

as 开发者_C百科the following code, my question is:

in testFunction, how can i call function remove?

in remove how can i call testFunction?

in remove, how can i call add?

great thanks

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

    }
}

};


To access remove() from testFunction() you can do:

this.classValue.remove();

To go the other way I think you'd have to use

stringhelper.testFunction();

as you don't have a variable containing the function or the parent this object (which you could use in a closure).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜