3
var foo = { x: 1, y: (function () { return ++this.x; })() }; console.log(foo.y); // undefined rather than 2 

Let's say that I want to be able to reference foo.y without using foo.y(). Is that possible?

The above obviously doesn't work, and I'm assuming it is because closure references a different this.

5
  • It doesn't "change" this at all, it is an IIFE (Immediately Invoked Function Expression), and doesn't get any other this context other than the global this, which is probably window, if you're working within the browser, as it seems you are. Commented Apr 2, 2012 at 2:28
  • are you wanting to foo.y to always return whatever x is as x changes, or just what it was initialized as? Commented Apr 2, 2012 at 2:29
  • @BradHarris Yes, let's say, for example, I wanted foo.y to always contain the value of ++this.x at any given moment, but I didn't want to use parens (e.g. foo.y()). Commented Apr 2, 2012 at 2:33
  • 2
    @oevna Your question is "How can I invoke a function without using ()?" Commented Apr 2, 2012 at 2:42
  • @LarryBattle Not at all. Commented Apr 2, 2012 at 2:44

5 Answers 5

6

If you want to access y as a property and not a function, and have it return the current value of foo.x, the only way I can think of is using getters/setters, which is part of the ES5 spec, and adopted in most of the modern browsers (but not really useable if you're supporting older browsers).

var foo = { x: 1, get y() { return this.x; } }; 
Sign up to request clarification or add additional context in comments.

Comments

2

You don't declare y as function, but as a result of a function call. It should be:

var foo = { x: 1, y: function () { return this.x; } }; 

Or, if you want just to assign to y value of x:

 var foo = { x: 1, y: this.x }; 

UPDATE: It is NOT possible to make y synonym of x. When you declare y - it can be a value or a function. It cannot be a reference to another value.

4 Comments

That assigns a function to y, not the value of x. In your example, I would have to use foo.y().
Then simply y: this.x if you want just value, but not a function.
That would work, it's just not what I'm trying to accomplish. I'm trying to assign the return value of a function to a property while referencing properties from that same object within that function.
You cannot do that. Only the first example in my answer. No other way.
1

Why not just return x directly if you don't want to use y() as a function? The only reason why you would return a function is you actually need to modify x's result.

var foo = { x: 1, y: (function () { return this.x; })()}; console.log(foo.x); // 1 

Comments

0
var foo = new Object(); foo.x = 1; foo.y = function() { return this.x; }; console.log(foo.y()); 

or if you're trying to invoke y while you define foo:

var foo = new Object(); foo.x = 1; foo.y = x; console.log(foo.y); 

3 Comments

Your first example uses foo.y(), and the second doesn't put the return value of a function into foo.y.
My second example was effectively what your example does. You have an immediately executing function that sets it value to y which is the same as x.
Sorry, I provided a confusing example. x could be anything referencing another property of the same object; for example, the value of x incremented by 1 (in my updated example).
0

foo.y isn't a function. foo.y is defined as foo.x.

3 Comments

I should have been clearer. I want to be able to reference foo.y, not foo.y(). The example code I provided was my failed attempt to accomplish that. I could remove the closure, remove the "instant call" to the anonymous function, and foo.y() would work, but that isn't the behavior I'm seeking.
Could you provide an example of how you would like to reference foo.y?
I revised my original question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.