Skip to content Skip to sidebar Skip to footer

How To Access This Function In My Object?

I have a function object: var myObj=function(){ }; myObj.prototype = { availableColor: function(i){ return 'red_'+i; } getColor: function(){ var c = availableC

Solution 1:

var myObj={
  availableColor: function(i){

      return"red_"+i;

  },
  getColor: function(){
    var c = this.availableColor('3');
  }
}

EDIT

Another approach:

var myObj=function(){

};

myObj.prototype.availableColor = function(i){
      return"red_"+i;
  };
myObj.prototype.getColor = function(){
    var c = this.availableColor('3');
return c;
};

b = newmyObj();
document.write(b.getColor());

Solution 2:

If you just want to add methods to myObj, just do:

myObj.availableColor = function(i){
  return"red_"+i;
}

myObj.getColor = function(){
   var c = this.availableColor('3');
}

The way you use prototype will make myObj an constructor: var o = new myObj(). myObj won't have those methods.

Post a Comment for "How To Access This Function In My Object?"