Calling one prototype method inside another in javascript

indianwebdevil picture indianwebdevil · Jan 8, 2012 · Viewed 40.1k times · Source
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 ?

Answer

bjornd picture bjornd · Jan 8, 2012

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.