Calling one prototype method inside another in javascript

JavascriptOopPrototype

Javascript Problem Overview


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 ?

Javascript Solutions


Solution 1 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionindianwebdevilView Question on Stackoverflow
Solution 1 - JavascriptbjorndView Answer on Stackoverflow