2

I have something like this:

var MyObject = function () { };

MyObject.prototype = {
    methodA: function() {
           methodB();
    }

    methodB: function() {
           // do something
    }
}

How do I get this working? i keep getting "Object has no method 'methodB'".

4
  • 1
    You need a comma after the close brace of methodA:{} as well as a this.methodB(); instead of just methodB(); Commented Dec 23, 2013 at 22:03
  • You should get a reference error actually. Commented Dec 23, 2013 at 22:05
  • I do have the comma after methodA:{}. this.methodB(); gives me an error in Chrome: Uncaught TypeError: Object #<Object> has no method 'methodB' Commented Dec 24, 2013 at 1:26
  • nevermind, i was calling it inside a $.post return method. Commented Dec 24, 2013 at 1:36

2 Answers 2

3

Try using this within methodA. For example:

var MyObject = function () { };

MyObject.prototype = {
    methodA: function() {
           this.methodB();
    },

    methodB: function() {
           alert('foo');
    }
};

var o = new MyObject();
o.methodA(); // foo

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work. In Chrome, i get the following error: Uncaught TypeError: Object #<Object> has no method 'methodB'
2
this.methodB();

this call will work as soon as you instantiate a new object using

var newObject = new MyObject();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.