0

I have a javascript class and I want to do the following

MyClass.prototype.foo = function() {
    return 0;
}

MyClass.prototype.bar = function() {
    return foo() + 1;
}

But when I run the program it says foo() is not defined.

I tried

MyClass.prototype.bar = function() {
    return this.foo() + 1;
}

and

MyClass.prototype.bar = function() {
    return MyClass.foo() + 1;
}

But that doesn't work either and results in the same error.

In the end, I want to use both methods when I create the instance of my class. What is the best way of doing this.

Thanks

edit: I called the method using the new keyword

var myInstance = new MyClass();
Ian Fell
  • 7
  • 3
  • Did you use `new` when creating the instance? `var d = new MyClass(); d.bar();` Also you want to use the `this.foo()` one as that will run the method for that instance. – Patrick Evans Oct 24 '14 at 23:40
  • 2
    `this.foo()` should work. how are you "instantiating" your object? – go-oleg Oct 24 '14 at 23:40
  • more on prototype and constructor functions can be found here: http://stackoverflow.com/a/16063711/1641941 – HMR Oct 24 '14 at 23:52
  • As said `return this.foo() + 1;` should work. It all depends how your calling `MyClass.prototype.bar`. The value of `this` is determined by how the function is *called*, not how it is defined. Unfortunately you don't show how you call it, so we can't really help you. – Felix Kling Oct 25 '14 at 00:06
  • Methods in javascript should generally be involved as in `obj.method()`. That will cause `this` to point to `obj` inside the method execution. `.prototype` is a holding place for methods that will become part of objects that are created with `new` of that type. If you're regularly trying to call a method on some other prototype, then your code structure is probably wrong. Static methods (methods that don't operate on a specific instance of an object and don't use `this`) probably don't belong on a `.prototype`. All your guesses show that you don't really understand these basic concepts. – jfriend00 Oct 25 '14 at 00:09
  • @Felix Kling I call it with the new keyword. In the code I was writing, I actually had a callback where 'this' was undefined and I got the error messages confused. I solved it by defining var _this = this; Sorry I wasn't more specific but you were all a great help. – Ian Fell Oct 25 '14 at 00:20
  • Yeah, I guess this question/solution is more appropriate then: [How to access the correct `this` / context inside a callback?](http://stackoverflow.com/q/20279484/218196). Would you say it's a duplicate? Can I close it as such? Better than having someone actually use the solution suggested in the answer here. – Felix Kling Oct 25 '14 at 00:22
  • Yes I would say it is a duplicate and you can close it as such. Thanks for the stackoverflow link and your help. – Ian Fell Oct 25 '14 at 00:23
  • It would also be good if you updated your question with how you called the method. – Felix Kling Oct 25 '14 at 00:44

1 Answers1

-2
var MyClass = function(){

}

MyClass.prototype.foo = function() {
    return 0;
}

MyClass.prototype.bar = function() {
    //return MyClass.prototype.foo() + 1;
    return this.foo() +1;
}

myClass = new MyClass()

myClass.foo() //0
myClass.bar() //1

edited per the comments below

brybott
  • 157
  • 9
  • 2
    `MyClass.prototype.foo()` won't do the same as `this.foo()`. The first won't have the right context (`this`), probably the global. `MyClass.prototype.foo.call(this)` would do right, but that's just silly. – Rudie Oct 24 '14 at 23:47
  • @IanFell: But it's wrong. – Felix Kling Oct 25 '14 at 00:04
  • @Rudie: With `MyClass.prototype.foo()`, `this` will refer to `MyClass.prototype`. – Felix Kling Oct 25 '14 at 00:06
  • @FelixKling That's even weirder than `global` =) "But it's wrong" made me laugh. Awesome. – Rudie Oct 25 '14 at 00:18