-3
var MyClass = function() { 
    //some functionality goes here 
};

MyClass.prototype.xyz = function() { 
    //some functionality goes here 
};

MyClass.prototype.abc = function() {
    var self = this;
    // here self is not working and 'this.xyz()' isn't working too.
    self.xyz();
    // It works if I use MyClass.prototype.xyz();
};

Can someone help with what I am missing here?

Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67
sekhar
  • 371
  • 2
  • 10
  • 3
    Add **Complete** code. – Tushar Apr 26 '17 at 07:48
  • Probably a duplicate of [How to access the correct `this` inside a callback?](http://stackoverflow.com/q/20279484/218196) – Felix Kling Apr 26 '17 at 07:48
  • 2
    The value of `this` depends **not** on how you define your function but on how you call your function. Since you don't show how you call the function `abc` we cannot help you – slebetman Apr 26 '17 at 07:49
  • 2
    The value of `this` depends on how you call a function. You haven't shown us how you call the function. You should provide a real [mcve]. – Quentin Apr 26 '17 at 07:49

1 Answers1

-1

I don't really know how you call your functions here, but this works perfectly:

var MyClass = function() { 
    console.log("constructor");
};

MyClass.prototype.xyz = function() { 
    console.log("xyz");
};

MyClass.prototype.abc = function() {
    console.log("abc");
    this.xyz();
};

let myClass = new MyClass();

myClass.xyz();
myClass.abc();

and the output is simple:

constructor
xyz
abc
xyz
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82