I have created an object through literals and instantiating using ‘new’ operator. But that’s not working. Here is my code:
var rectangle = {
upperLeft : { x : 2, y : 2 },
lowerRight : { x : 4, y : 4}
}
var r=new rectangle;
alert(r.upperLeft.x) // will yield 2
I have tried using Parentheses after the object name while instantiating:
var r=new rectangle;
But that’s also not working. I have tried creating object using function statement and that’s working fine, but I want to do it this way. Help.
rectangleis, as you noticed, anObject Literal. It’s not a Constructor function, it’s a single instance ofObject. If you wantrectangleto have more instances, use something like: