3

Option 1:

NotificationsService.push = function (notifications) {}

Option 2:

NotificationsService.prototype.push = function (notifications){}

Whats the difference between in defining a function directly vs on the prototype chain? Is it the inheritance?

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
  • I recommend you to read this: http://www.javascriptenlightenment.com/, it's great to bring light in the prototype darkness – bukart Nov 03 '13 at 20:34

4 Answers4

2

What is NotificationsService here?

If it is a function, then the difference is that in the second case, every instance of NotificationsService will inherit push.

var instance = new NotificationsService();
instance.push(...);

In the first case, you simply extend NotificationsService and it doesn't have any effect on instances created by it:

var instance = new NotificationsService();
instance.push(...); // will throw an error
NotificationsService.push(); // will work

If NotificationsService is an object, and we assume that NotificationsService.prototype exists and is an object, then it doesn't have anything to do with the prototype chain, you simply define the function at two different locations. This is a simpler example:

var foo = {};
var foo.prototype = {};

// defines a method on foo
foo.push = function() {...};

// defines a method on foo.prototype
foo.prototype.push = function() {...};

Those two properties don't have any relation to each other though.


To conclude: In both cases you are defining the method on different objects and as a consequence, have to be used differently. What to do depends on your use case.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • `NotificationsService` is function. this does inheritance in prototypical way? So no need of classes...Cool. Thanks for your answer, it was helpful & to the point. – CuriousMind Nov 04 '13 at 18:09
1

first case will affect only current instance of the object you're declaring

var NotificationsService=new WhateverService();
NotificationsService.push=function(notifications) { 
    console.log('Instance function',notifications) 
};
NotificationsService.push('hello')

Instance function Hello

second case should be applied on the parent 'Class' (altough js has no classes) in this case WhateverService

WhateverService.prototype.push=function(notifications) {
 console.log('Prototype function',notifications);   
}
var NotificationsService=new WhateverService();
NotificationsService.push('hello')

Prototype function Hello

If you declare a second instance of WhateverService it will inherit any method bound to the parent's prototype, and no method attached directly to a sibling instance.

ffflabs
  • 17,166
  • 5
  • 51
  • 77
1

the best advice I can give you is to read this one it has one of the best explanations of prototyping I have seen so far,but just for the sake of mentioning, using both will give you the same end result but with different approaches and my preference is to use the first one unless you really need a prototype for this.

Community
  • 1
  • 1
Labib Ismaiel
  • 1,240
  • 2
  • 11
  • 21
1

Protoype is equal extends of your object.

If use in class A

var b = new A()
b.prototype.job = "Nothing"

var c = new B()

console.log(c.job); //Nothing

//All variable have instance A and the next instances from A it'll have the prop job

but if only put

b.job = "Nothing";

variable C don't have prop "job"

console.log(c.job); //undefined
Guilherme Soares
  • 726
  • 3
  • 7
  • 19