Objetos litrales La instanciación no funciona

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.

preguntado el 10 de marzo de 12 a las 08:03

4 Respuestas

Probablemente te refieres

var r = Object.create(rectangle);

Aquí es: http://jsfiddle.net/Cra8Q/

APÉNDICE

The "old" way in JavaScript to create a bunch of rectangles is to use the new operator. The way to do that is like this:

function Rectangle(x1, y1, x2, y2) {
    this.upperLeft = {x: x1, y: y1};
    this.lowerRight = {x: x2, y: y2};
}

var r = new Rectangle(2, 2, 4, 4);

alert(r.upperLeft.x);

Vea http://jsfiddle.net/uDs3N/

The problem with the code in the question was that rectangle was an actual rectangle object and not a constructor, so it could not be used with new. El operador new can only be used before a function call.

That said, the more modern approach is to go with the creation of the prototypical rectangle object, and derive new rectangles with Object.create. However, the constructor approach is still popular.

respondido 10 mar '12, 09:03

Yeah that's working but whats the problem with using new operator? and Will constructors work this was? - Sourabh

Oh, if your intent was to use constructores I will add that to my answer. - rayo toal

rectangle is, as you noticed, an Object Literal. It's not a Constructor function, it's a single instance of Object. Si tu quieres rectangle to have more instances, use something like:

function Rectangle(ul,lr){
  var default = {x:0,y:0};
  return {
          upperLeft: ul || default, 
          lowerRight: lr || default 
         };
}

var r1 = new Rectangle({x:2,y:2},{x:4,y:4}),
    r2 = new Rectangle({x:3,y:3},{x:5,y:5}),
    r3 = new Rectangle;

r1.upperleft.x; //=> 2
r2.upperleft.x; //=> 3
r3.upperLeft.x = 5;
r3.upperLeft.y = 7;
alert(r3.upperLeft.x; //=> 5

respondido 10 mar '12, 09:03

The other way to create an object with "new" operator is to define a constructor function, like this:

var Rectangle = function()
{
    this.upperLeft = { x : 2, y : 2 },
    this.lowerRight = { x : 4, y : 4}
}

var rect = new Rectangle();
alert(rect.upperLeft.x);

respondido 10 mar '12, 09:03

If you have read the question, I have mentioned that I have tried creating object using function statement and that's working fine, but I want to do it this way. Thanx - Sourabh

You can't use "new" with object literals in pure JS. Only with functions. To create an object from a literal you should create a hand-made "clone" function or use already provided in frameworks for example. - Just_Mad

El new operator takes a función, not an object. What you're trying won't work.

Esta es la forma new obras:

function Rectangle() {
    this.upperLeft = {x: 2, y: 2};
    this.lowerRight = {x: 4, y: 4};
}

var r = new Rectangle();

Part of the operation of var r = new Rectangle() is similar to this:

var r = {};
Rectangle.call(r); // makes r available as `this` in the function

Es far from equivalent to what new; it's leaving out all kinds of prototype magic and other things.

But it shows why the argument to new needs to be a function.

respondido 10 mar '12, 09:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.