1

I'm trying to create an object in which some properties rely on other previously defined properties, while trying to rely on object literals. Otherwise put, I'm trying to accomplish something like the following code (which is incorrect):

var mesh = {
            offset   : $('#mesh').offset(),
            position : $('#mesh').position(),
            height   : win.height - this.offset.top - 12,   
            width    : win.width - this.offset.left - 12,   
            limits   : {}
        }

as opposed to something of this sort (which works):

var mesh = {};
        mesh.offset   = $('#mesh').offset();
        mesh.position = $('#mesh').position();
        mesh.height   = win.height - mesh.offset.top - 12;  
        mesh.width    = win.width - mesh.offset.left - 12;
        mesh.limits = {};

So my question is rather simple: what is actually wrong with the first block of code and how can I correct it in order to create those new properties based on previously defined ones?

2 Answers 2

3

There is no name that references the currently-building object literal. You'll need to use the second form, or have the values in variables that you can reference:

var offset = $('#mesh').offset();
var mesh = {
    offset   : offset,
    position : $('#mesh').position(),
    height   : win.height - offset.top - 12,   
    width    : win.width - offset.left - 12,   
    limits   : {}
}
Sign up to request clarification or add additional context in comments.

6 Comments

And there's no other way of accomplishing what I need?
There a few ways. Have a look at stackoverflow.com/…
There's no way of accomplishing what you ASKED, which is using object literal notation AND reusing previously defined attributes in the current object definition. There are many ways to achieve something that is functionally equivalent, and you provided one such way yourself.
BTW, Ned, would you please wait a few minutes before giving perfect answers, so the rest of us get a chance ;-)
@Felix Kling Thanks Felix. Those are actually the closest solutions to what I needed. Yet it seems that what I'm trying to accomplish is in fact, impossible.
|
0

you can create a constructor function

var Mesh = function(mesh_id){
    var mesh = $(mesh_id);
    this.offset = mesh.offset();
    this.position = mesh.position();
    this.height = win.height - this.offset.top - 12;
    this.width = win.width - offset.left - 12
    this.limits = {}


};

and then use it like this

var myMesh = new Mesh('#mesh')

*note that this code is assuming some variable win is defined. you have probably defined it as var win = $(window)

3 Comments

This is not answering the question, which is a about achieving this result using object literal notation.
Indeed, this has nothing to do with taking advantage of the literal notation. I am in fact trying to avoid using constructors.
thats true ddaa. However, as you stated in your other comment, there is no way to do what he asked while sticking with an object literal. This is another alternative, though not exactly functionally equivalent, as I believe the memory gets allocated differently with constructor functions.

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.