Jinu Joseph Daniel
Jinu Joseph Daniel

Reputation: 6291

Creating new objects using object literals

I have the following object literal:

var a = {a:1,b:2}

now I want another instance of the same object. If I'm using a constructor, I can do this using the 'new' operator, ie:

b = new a(); 

How to create a new instance of an object using object literals?

Upvotes: 2

Views: 3600

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83358

The simplest way would be with Object.create

var b = Object.create(a);

console.log(b.a); //1
console.log(b.b); //2

DEMO

And of course if you need to support older browsers, you can get the MDN shim for Object.create here

Upvotes: 3

Related Questions