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.

11
  • Maybe the advantage of using Object.create is that it does work, while your .prototype code does not? Commented Jun 27, 2015 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. Commented Jun 27, 2015 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) Commented Jun 27, 2015 at 17:57
  • @RoyiNamir: Well he certainly is confusing constructor functions with instances for C. Commented Jun 27, 2015 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 (jsbin.com/dahuzu/edit?html,js,output) Commented Jun 27, 2015 at 18:06

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

1 Comment

In contrast, the notable advantage of using constructors+.prototype over Object.create is that they provide a means to initialise all instances on creation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.