Calling one prototype method inside another in javascript

42,210

It's easy:

Ob.prototype.add = function(){
    this.inc()
}

Ob.prototype.inc = function(){
    alert(' Inc called ');
}

When you create the instance of Ob properties from prototype are copied to the object. If you want to access the methods of instance from within its another method you could use this.

Share:
42,210
indianwebdevil
Author by

indianwebdevil

Curious To Know who am I ? Knowing What I am Doing might be of some use. Find me on twitter, or read my blog. Built : PutForShare.com RolloutCD.com - Single Click Web App Deployment Tool with no scripting required

Updated on July 08, 2022

Comments

  • indianwebdevil
    indianwebdevil almost 2 years
    var Ob = function(){
    
    
    }
    
    Ob.prototype.add = function(){
        inc()
    
    }
    
    Ob.prototype.inc = function(){
        alert(' Inc called ');
    
    }
    
    window.onload = function(){
    var o = new Ob();
    o.add();
    }
    

    I would like to call something like this,how can i call, ofcourse i put inc as inner function to add I can do that but without having the inner function. how do i do that ?