0

Excuse my terminology if it's off. I don't understand the difference between:

function Person() {};
Person.walk = function() {};

and...

function Person() {};
Person.prototype.walk = function() {};

It seems that the second way is the convention for constructors, but I don't understand the difference and why it is done that way. Thanks!

natecraft1
  • 2,737
  • 9
  • 36
  • 56
  • The first one is like a static method, the second one depends on an instance. – elclanrs Jan 27 '14 at 04:47
  • Like some of the answers clarify, the first is a static method and second is method you can invoke on instances. For more info about constructor functions and prototype: http://stackoverflow.com/a/16063711/1641941 – HMR Jan 27 '14 at 05:31

3 Answers3

3

In the first case:

function Person() {};
Person.walk = function() {};

You will be able to call the function only with:

Person.walk();

If you create an instance of Person, you won't be able to call the method:

p = new Person();
p.walk() // -> TypeError: Object #<Person>  has no method 'walk'

In the other hand, if you use the prototype you will only be able to call the method through an instance.

S. A.
  • 3,714
  • 2
  • 20
  • 31
1

The first function has no relationship with an object instance of that constructor function, you can consider it like a 'static method'.

The second function, as you are extending the constructor function prototype, it will be available to all the object instances created with the new keyword

Example:

// constructor function

function MyClass () {
  var privateVariable; // private member only available within the constructor fn

  this.privilegedMethod = function () { // it can access private members
  //..
  };
}

// A 'static method', it's just like a normal function 
// it has no relation with any 'MyClass' object instance
MyClass.staticMethod = function () {};

MyClass.prototype.publicMethod = function () {
// the 'this' keyword refers to the object instance
// you can access only 'privileged' and 'public' members
};

var myObj = new MyClass(); // new object instance

myObj.publicMethod();
MyClass.staticMethod();
Vinoth
  • 657
  • 6
  • 12
1

The prototype is kind of a template for creating instances, unlike the constructor. Note that prototype's members are not actually copied to instances, instead, instances draw on prototype's resources, unless the resource is already owned by the instance.

var Class = function () {};
Class.prototype.a = 1;
Class.prototype.b = 2;

// creates a new instance named "c"
var c = new Class();
c.b = 3;

console.log(
    c.hasOwnProperty('a'), // -> false
    c.hasOwnProperty('b'), // -> true
    c.a, // -> 1 (prototype)
    c.b, // -> 3 (instance)
    Class.prototype.b // -> 2 (prototype)
);
  • what do you mean by "prototype's members are not actually copied to instances, instead, instances draw on prototype's resources". when I type c in the console after making var c = new Class() it shows that c is an object with an "a" and "b" property. doesn't that mean those prototype properties (or members... are properties/members the same thing?) are part of that c object? – natecraft1 Jan 28 '14 at 00:15
  • @natecraft1 I couldn't explain this better : http://stackoverflow.com/a/572996/1636522 :D –  Jan 28 '14 at 06:00