-3

What is the diference in the below aproachs. Which is the best way?

    var obj;
        obj = function () {

        }
        obj.prototype = {
         getName: function () {
         console.log("my name");
        }
     }

and this

 var obj;
    obj = function () {

    }
    obj.getName.prototype = {
    console.log("my name");
    }

Are both the same?

Leonel Matias Domingos
  • 1,922
  • 5
  • 29
  • 53
  • 1
    Well, just try it out. The second one throws an exception, so it's obviously not the same. – Bergi Feb 07 '16 at 13:19

3 Answers3

1

With first one.

You are overwriting complete prototype object with new one.So,existing method and properties are get removed because of this assignment.

With Second one

You are assigning new method and not removing existing method and properties.

EDIT :

The second one should be like this

obj.prototype.getName = function(){
}
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • The second on is just a syntax error. (And if you add `function()`, it's still a type error) – Bergi Feb 07 '16 at 13:34
1

They are not the same, and valid way to do it would be the first way.

Perhaps what you wanted to ask instead was the difference between these:

function() Obj {}

Obj.prototype.getName = function() {
       console.log("my name");
};

var obj = new Obj;

vs this

function() Obj {
  this.getName = function() {
     console.log("my name");
  };
}

var obj = new Obj;

well the answer is both are valid, first one adds function to prototype chain and second one adds to instance of the function.

Let's start with second one, simpler. After you run the code obj will have function name getName Attached to itself. obj.getName() will call the method on obj.

Whereas in first one when you call obj.getName(), the js compiler will look at obj for method getName and it will not be found there, so it will try look it up the chain. All objects in js have __proto__ property. Where another object is. When you create object trhough a function using new keyword, resulted object's __proto__ is set to fucntion's prototype.

The advantage of delegating functions like this is that say you make 30 objects with this function then there wont be 30 getName methods as well. Instead, all object will still be referencing to prototype object which will have the getName method. So there will be 30 references.

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
0

No they are not the same.

The Code you posted is not valid JavaScript. And raises a SyntaxError. I can only assume you ment to ask which one of the following is the "better" way of assinging properties to an Object Prototype:

var foo = {}
foo.prototype = {
   getName: function(){}
}

vs:

var foo = {}
foo.prototype = {}
foo.prototype.getName = function(){}

The first approach is better since you are not re-assinging a new object to the prototype - it would remove all properties that might have existed. So the first one is considered better.

soundyogi
  • 369
  • 3
  • 15
  • 1
    (actually [the first one *is* better](https://stackoverflow.com/questions/17474390/defining-a-javascript-prototype)) – Bergi Feb 07 '16 at 13:21