-2

How can I call a prototype function from the object's main function/constructor in javascript. I tried the following, but it does not work. What am I doing wrong?

var x = new myFunction('Hello World!');

function myFunction(name) {
    this.name = name;

    alert( toString() ); // not working
    alert( this.toString() ); // not working either
};

myFunction.prototype.toString = function() {  
    return 'My name is ' + this.name;
};
Christian
  • 1,589
  • 1
  • 18
  • 36
  • You need to invoke `myFunction` using `new`, otherwise `this` won't be what it needs to be. `var func = new myFunction("foo"); console.log(func.toString());` – Matt Aug 19 '14 at 15:03
  • 3
    The second one works. Just do `new myFunction('test')`. – Denys Séguret Aug 19 '14 at 15:04
  • I updated my code above. Now I create an instance of myFunction. The calls still won't work. Can you please supply a working example as an answer? Btw: Don't understand the downvotes! No one came up with an obvious and working answer yet!? – Christian Aug 19 '14 at 16:28
  • @Matt: Of course your code will work. Calling the toString-function from outside works just fine! How can I use that function from inside the object itself. – Christian Aug 19 '14 at 16:29

1 Answers1

1

You're creating an instance of myFunction and call toString on it before you set the prototype.

The reason you can create an instance of myFunction even before you declare it is because it was hoisted. toString is not hoisted however and it would show [Object object].

Solution is to create instances after you fully declare the object.

Note: A constructor function should start with a capital so it should be MyFunction instead of myFunction and maybe give it a name that actually means something like Person or Animal since nobody would have a clue to what a MyFunction is.

function myFunction(name) {
    this.name = name;
    //console.log is so much better than alert
    console.log('this is:',this,'this.toString:'
      , this.toString() );
};
myFunction.prototype.toString = function() {  
    return 'My name is ' + this.name;
};
var x = new myFunction('Hello World!');

More on prototype here: Prototypical inheritance - writing up

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160
  • Hi HMR. Yes, Firebug console output says that toString is not a function. So what would be a proper and working solution? Can you give me a working example? – Christian Aug 20 '14 at 06:41
  • @christian Take out that line – HMR Aug 20 '14 at 06:47
  • Then how and where would I call toString() instead? – Christian Aug 20 '14 at 08:28
  • @Christian I was wrong with the toString causing an error so unless you're now using code you didn't post in your question something strange is going on. I've updated my answer. You can copy the code and paste it in the console at the bottom to run it. – HMR Aug 20 '14 at 10:24
  • k, got it. That is weired. If I use MY code above it does not work. But when I move the line `var x = new myFunction('Hello World!')` to the bottom, it works. Any idea why? – Christian Aug 20 '14 at 13:28
  • @Christian Yes, it's explained in the answer. Variable hoisting is a weird thing. – HMR Aug 20 '14 at 13:57