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.
Object.createis that it does work, while your.prototypecode does not?Object.createis just what you use to inherit from an existing object, whilefunction C(){}; C.prototype = A;andvar c = new C(); c.lastname; c.getname()has the advantage of executing code to initialise instances.C.prototype = A;the prototype he is referencing is not the one he needs.C.constructor.prototypeis the one he's seeking. ( which I don't recommend)C.Object.prototype=A;? doesn't work here. only if you manually add keys one by one (jsbin.com/dahuzu/edit?html,js,output)