views:

72

answers:

1

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

 }
}

};

+2  A: 

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).

Greg
Why not `stringhelper.classValue.remove()`? This way, aliasing of functions won't mess up `this` reference.
kangax