0

I am learning javascript inheritance so I am trying the following code:

var obj = function(name) {
  this.name=name
};

var obj1 = new obj("suman"); 
console.log(obj1.name); 

var obj2 = new obj1("sonali"); 
console.log(obj2.name);

But when I try getting obj2.name I get the following error object is not a function.

As per my understanding var obj1 is inheriting properties from obj . So when I am inheriting obj2 from obj1 it should inherit name property also. Please guide me where I am wrong.

5
  • 1
    Simplest way is to use obj1 as the prototype for obj2: var obj2=new Object(obj1);. Commented Jun 24, 2015 at 10:56
  • 1
    because obj1 is not a new object but an instance of obj. to see multiple inheritance sample go to : stackoverflow.com/questions/9163341/…, for article on java script inheritance see: phrogz.net/JS/classes/OOPinJS2.html Commented Jun 24, 2015 at 11:04
  • where did you find this code ? Commented Jun 24, 2015 at 11:08
  • You can not inherit from an object. You can inherit from class. In your case class it's a function obj. Commented Jun 24, 2015 at 11:12
  • What exactly are you trying to accomplish with this code? Commented Jun 24, 2015 at 11:18

2 Answers 2

4

but when I am trying for obj2.name, I am getting error object is not a function.

Not exactly. You're getting that error when you call var obj2=new obj1("sonali"); - where obj1 is the first instance, not your constructor. Use

var obj2 = new obj("sonali");
console.log(obj2.name); // works as expected

For better readability, the convention is to capitalise the names of constructor functions, so function Obj(name){…} and var obj = new Obj(…).

per my understanding obj1 is inheriting properties from obj

No, obj1 is an instance. It does have the .name property on itself, it doesn't inherit it, it does own it. obj is the constructor function that created the property as part of the instance initialisation.

obj1 does inherit properties from obj.prototype - since you didn't create any custom ones, the only inherited property is obj1.constructor.

So when I am inheriting obj2 from obj1 it should inherit name property also.

As stated above, that's not what's happening. obj1 is not a constructor function, you cannot call it with new. I bet I'm confusing you now, but you can still inherit from the obj1 instance object though: by using Object.create. Let's start with

function Obj(name) {
  this.name = name;
}
var obj1 = new Obj("suman"); // or alternatively:
obj1 = Object.create(Obj.prototype); obj1.name = "suman";

console.log(obj1.name); // suman

Now we can create a new object, inheriting prototypically from the first object:

var obj2 = Object.create(obj1);
console.log(obj2.name); // suman - inherited!
console.log(obj2.hasOwnProperty("name")); // false - it's inherited!

But now we can give it an own name as well:

obj2.constructor("sonali"); // which executes the following:
obj2.name = "sonali"; // pretty equivalent

console.log(obj2.name); // sonali
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Bergi it really helped me to understand
2

You are confusing the concepts of 'class' and 'instance' which have specific meanings in Object Oriented literature.

In your example, obj refers to the class/protoype or broadly speaking, a "template" that is used as a reference for creating copies. The object reference provided in the statement using the new operator is the 'instance' created for the class. So, obj1 is a valid instance.

So, only classes can be instantiated and not their instances. To answer your question instantiating a class does not lead to the child 'inheriting' the ability to be instantiated further.

Hence, the objects you wish to create should always refer the 'class' you wish to use while calling the constructor with the new keyword.

Comments

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.