1

I'm running into problems when I'm trying to call a function on an object from within that same object. I've read that calling a function on an object from within that object is possible here, so I think it must be my use of prototype that's causing the issue. Here's an example:

function Foo() {
    this.DoStuff();
}

Foo.prototype.DoStuff = function() {
    alert("I'm frantically doing stuff!");
}

That code (or something very similar) just doesn't want to work. Any ideas why?

1
  • Just tested your code and it works fine. Post an example that actually shows the issue please :( Commented Mar 7, 2012 at 21:57

3 Answers 3

5

What you have should work fine. It's important to remember that the value of this depends on how you call the function. For it to work as you expect, you need to use the new operator to call the function as a constructor:

var foo = new Foo(); //`this` refers to this instance of Foo

Here's a working example.

If you call the function like normal, this refers to the global object, which doesn't have a DoStuff property, so a TypeError is thrown. Here's a broken example.

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

4 Comments

I was indeed using the new keyword correctly, which is why I'm confused as to why my code isn't working.
@ElliotBonneville - Can you make an example on jsfiddle.net that demonstrates your problem?
Yes. I'll get back to you in a few minutes.
Actually, I've located the problem. It was another issue entirely-- the function I wanted to call on my object involved some DOM manipulation, and I was creating the object before the DOM finished loading. Once I moved the line creating the object to a place such that it would only be called after the DOM was initialized, my problems were solved. Even though your answer technically didn't provide me with a solution, I'm accepting it because it proved the most helpful. Thanks! :)
0

I'm going to guess that your prototype statement is declared after you try to use Foo().

This should work:

Foo.prototype.DoStuff = function() { alert("stuff") };
myfoo = new Foo();

This should NOT:

myfoo = new Foo();
Foo.prototype.DoStuff = function() { alert("stuff"); };

4 Comments

Nope, that's not the case. I have my code very neatly structured (if I do say so myself) and I don't declare any objects or variables before I create my object and prototype statements. Thanks for the help, though!
If I had to make another guess, I would question how you were calling Foo (James Allardice mentions this above). If that's the case and you can't call it any other way, you might want to check into bind.
Actually, I've solved the problem. :) See my comment on James Allardice's answer. Just out of curiosity though, what do you mean by 'check into bind'?
Good to hear! Bind in Javascript allows you to bind the "this" keyword to a specific object. Regardless of how the function is called, "this" will always be the object that you have bound it to. Here's a link from MDN - developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
0

It depends on how you call Foo(). If you call it directly, then this refers to whatever this is in that context. If you call it with the new operator, then this becomes that instance of that object, and then it has access to DoStuff().

var foo = new Foo(); // works!

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.