tags:

views:

24

answers:

1

I hope this will work

var Human = function() {
   this.name = "baby";
   this.age = 0;
}
var Man = function() {
   this.sex = male;
}
Man.prototype = Human;

But

var Human = {
    name:"baby",
    age:0
};
var Man = {
    sex:"male"
};

How to inherit one object from another while objects are created using object literals in javascript?

A: 

Object literals are just Objects, you can't create many instances of them unless you wrap them with the new keyword.

Here's Crockford's version of creating many instances of Object literals:

function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

var Human = {
    name:"baby",
    age:0
};
Human = object(Human);

var Man = {
    sex:"male"
};
Man = object(Man);

Now that both of your Object literals have an instance, you can do inheritance just like in your 1st example.

Luca Matteis