0

I have a prototype:

var myClass = function() {};

I have a method on that class:

myClass.prototype.method1() = function() {
   return x;
}

I then have a second method which I declare in the same way, but I want to access the result of method1() in it. But this doesn't work, and I'm not sure why...

myClass.prototype.method2() = function() {
   return myClass.method1();
}

I get TypeError: myClass.method1 is not a function

Can anyone point out what's syntactically/conceptually wrong with this? And suggest any improvements?

Also I'm not 100% I'm using the terms 'class' and 'prototype' correctly in this instance. If anyone can correct the terminology, or point out any other terminology associated with this kind of process I'd appreciate it!

Thanks in advance.

Paulos3000
  • 2,717
  • 7
  • 30
  • 63
  • 2
    it's because *myClass* doesn't exist... What you probably want is to use `this.method1()`. Beware of how `this` works, though http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work/3127440#3127440 – VLAZ Aug 07 '16 at 11:38
  • 1
    `var myClass = function() {};` is not a "prototype". It's a constructor. –  Aug 07 '16 at 11:42
  • Thanks Vid - that's sorted my problem in this instance, + 1 – Paulos3000 Aug 07 '16 at 11:50
  • Cheers for the terminology pointer torazaburo, I had a feeling it might be a constructor. I hear these terms floating about but never 100% sure when to use them... – Paulos3000 Aug 07 '16 at 11:51

3 Answers3

1
var myClass = function() {};

This is a constructor and not a prototype. Here's a quick read explaining the difference.

You can call it using this. Here's the best place for a brush up.

const MyClass = function() {};

MyClass.prototype.method1 = function() {
   return x;
}

MyClass.prototype.method2 = function() {
   return this.method1(); 
}

Also, no need for brackets in function definitions of method1 and method2

Vandesh
  • 4,638
  • 1
  • 20
  • 30
  • Actually that was stupid of me, I knew that already, just a careless typo (re: using brackets after `method1` and `method2`). The `this` keyword is what I needed though. Thanks! – Paulos3000 Aug 07 '16 at 11:53
0

That's because (using rough OO terminology), method1() is an instance method, and you're trying to call it as a static method.

const MyClass = function() {}; // MyClass is a constructor

MyClass.prototype.method1() = function() {
   return x;
}

MyClass.prototype.method2() = function() {
   return this.method1(); // Call it as an instance
}

or, alternatively:

const MyClass = function() {};

MyClass.method1() = function() { // "static" method
   return x;
}

MyClass.prototype.method2() = function() {
   return MyClass.method1(); // Call it as a "static"
}

Worth noting that this terminology is misleading, there are no static or instance methods (in fact, there are no methods in JavaScript, only functions, and properties that hold references to functions).

Madara's Ghost
  • 158,961
  • 49
  • 244
  • 292
0

The JS oop has some difference between public and privileged methods.

myClass.method1(); is A static method calling. it's just like a normal function and has no relation with any myClass object instance.

So you need to call it using the Full namespace:

MyClass.method1()

Riad
  • 3,698
  • 5
  • 26
  • 38
  • Agreed, please explain. My original post and the one I marked as solution were also downvoted for no apparent reason. Have +1. – Paulos3000 Aug 07 '16 at 13:06