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 :(
    – david
    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.

4
  • I was indeed using the new keyword correctly, which is why I'm confused as to why my code isn't working. Commented Mar 7, 2012 at 21:56
  • @ElliotBonneville - Can you make an example on jsfiddle.net that demonstrates your problem? Commented Mar 7, 2012 at 21:57
  • Yes. I'll get back to you in a few minutes. Commented Mar 7, 2012 at 22:00
  • 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! :) Commented Mar 7, 2012 at 22:12
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
  • 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! Commented Mar 7, 2012 at 21:56
  • 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. Commented Mar 7, 2012 at 22:08
  • 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'? Commented Mar 7, 2012 at 22:14
  • 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/… Commented Mar 7, 2012 at 22:24
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!

Not the answer you're looking for? Browse other questions tagged or ask your own question.