2

I am new to JS.

I was experimenting in JSFiddle.

I have created an object A and then I have created two new Objects B and C like below.

debugger;
var A = {
    name:"h",
    lastname:'n',
    address:'B'
};
A.getname = function()
{
    console.log(this.name);
};
var B = Object.create(A);
var C=new Object();
C.prototype = A;
console.log(B.lastname);
console.log(C.lastname);
A.getname();
B.getname();
C.getname();

I have taken the concept of creating a new object with already existing object using Object.create(old object) from javascript:Good Parts book and the concept of inheriting object from http://www.codecademy.com/courses/objects-ii/3/3?curriculum_id=506324b3a7dffd00020bf661 .

After debugging, But I the value console.log(C.lastname) is undefined and C.getname() is giving me error.

TypeError: C.getname is not a function

Why is it throwing me error and what are the advantages of using Object.create() in this case.

sfletche
  • 47,248
  • 30
  • 103
  • 119
starkk92
  • 5,754
  • 9
  • 43
  • 59
  • Maybe the advantage of using `Object.create` is that it does work, while your `.prototype` code does not? – Bergi Jun 27 '15 at 17:53
  • But I think the answer you're actually looking for is that `Object.create` is just what you use to inherit from an existing object, while `function C(){}; C.prototype = A;` and `var c = new C(); c.lastname; c.getname()` has the advantage of executing code to initialise instances. – Bergi Jun 27 '15 at 17:56
  • @Bergi I believe the problem ( among others) is here `C.prototype = A;` the prototype he is referencing is not the one he needs. `C.constructor.prototype` is the one he's seeking. ( which I don't recommend) – Royi Namir Jun 27 '15 at 17:57
  • @RoyiNamir: Well he certainly is confusing constructor functions with instances for `C`. – Bergi Jun 27 '15 at 18:00
  • @Bergi Is it even valid to do `Object.prototype=A;` ? doesn't work here. only if you manually add keys one by one (http://jsbin.com/dahuzu/edit?html,js,output) – Royi Namir Jun 27 '15 at 18:06
  • @RoyiNamir: [No, it's not](http://es5.github.io/#x15.2.3.1). You'll get an error in strict mode. – Bergi Jun 27 '15 at 18:22
  • @RoyiNamir So this would be like Object.prototype=A as the object C is instance of Object()? and is this failing as Object.prototype=A is invalid? – starkk92 Jun 27 '15 at 18:29
  • @starkk92: Wait. What did you expect `C.prototype = A;` to do actually? Make `C` inherit from `A`? – Bergi Jun 27 '15 at 19:00
  • @Bergi yes.I think Object.create() also does the same if I am not wrong. – starkk92 Jun 27 '15 at 19:42
  • @starkk92: Yes, `Object.create` does this. But `.prototype =` doesn't, that's an ordinary property assignment/creation! You'd need to use [`Object.setPrototypeOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf) instead. – Bergi Jun 27 '15 at 19:49
  • see also the possible duplicate http://stackoverflow.com/q/9959727/1048572. `.prototype` is only used on constructor functions! – Bergi Jun 27 '15 at 19:51

1 Answers1

2

If you want to use .prototype for inheritance, you'll need A and C to be classes (not just objects).

var A = function() {
    this.name = 'h';
    this.lastname = 'n';
    this.address = 'B';
}
A.prototype.getname = function() {
    console.log(this.name);
};
var a = new A(); 
console.log(a.getname()); // h

var C = function() {};
C.prototype = new A();
var c = new C();
console.log(c.lastname); // n
console.log(c.getname()); // h

One notable advantage of using Object.create over Object.prototype for inheritance is (as can be seen here) that with Object.create you can use simple objects (rather than classes).

To better understand advantages of using Object.create see @FelixKling's accepted answer to a similar question...

Community
  • 1
  • 1
sfletche
  • 47,248
  • 30
  • 103
  • 119
  • In contrast, the notable advantage of using constructors+`.prototype` over `Object.create` is that they provide a means to initialise all instances on creation. – Bergi Jun 27 '15 at 19:52