Paulos3000 Paulos3000 - 1 year ago 61
Javascript Question

Calling one method of an object in another

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
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.

Answer Source
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download