Here is an example can anybody tell me how to do this
var calculation = function(){
this.Add = function(){
}
this.Subtract(){
var add = function(){
//want to access Add function here
}
}
}
You can simply use a variable to refer to this and you can use that variable latter.
var calculation = function() {
var _this = this;
this.Add = function() {
alert('In Add function');
}
this.Subtract = function() {
var add = function() {
//want to access Add function here
_this.Add();
}
add();
}
};
var cal = new calculation()
cal.Subtract()
this.Subtract = function() { and OPs this.Subtract(){var _this = this; when using this in the function. Otherwise I'll use the bind method. See my answer.Try this:
vvar calculation = function(){
this.Add = function(){
alert("test");
}
this.Subtract = function(){
this.Add();
}.bind(this)
}
var sum = new calculation();
sum.Subtract();
bind is core JavaScript, it doesn't come from jQuery.