How to call object's method from constructor?

Posted by Kirzilla on Stack Overflow See other posts from Stack Overflow or by Kirzilla
Published on 2010-04-15T10:10:36Z Indexed on 2010/04/15 10:13 UTC
Read the original article Hit count: 179

Filed under:
|

Hello,

var Dog = function(name) {
  this.name = name;
  this.sayName();
}

Dog.prototype.sayName = function() {
  alert(this.name);
}

I'm creating new instance of Dog object, but method sayName() is undefined. Why?

Or maybe I should do something like (but I can't see difference)...

var Dog = function(name) {
  this.name = name;
  this.prototype.sayName = function() {
    alert(this.name);
  }
}

Thank you.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about oop