Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This setter approach cannot work. No matter what the function returns, if you use a function call as sole expression left-hand side (LHS) of an assignment expression, you are getting</p> <blockquote> <p>ReferenceError: Invalid left-hand side in assignment</p> </blockquote> <p>and the like. As the others have said, you are not seeing the <code>ReferenceError</code> exception because it is caught as it is being thrown and there is no code to handle it. Functions are first-class objects, but <em>calls</em> to them are <em>not</em> <a href="http://ecma-international.org/ecma-262/5.1/#sec-11.13.1" rel="nofollow">valid LHS values</a>. You will see that clearly if you insert <code>console.log(e);</code> in the <code>catch</code> block.</p> <p>This getter approach can work. However, if you want getters and setters, at the latest in a conforming implementation of ECMAScript Edition 5 <a href="http://ecma-international.org/ecma-262/5.1/#sec-15.2.3.6" rel="nofollow">you can have that natively</a>:</p> <pre><code>Object.defineProperty(props, "name", (function () { var _value; return { set: function (value) { /* setter code, simple example */ _value = String(value); }, get: function () { /* getter code, simple example */ return _value; } }; }())); /* triggers the setter */ props.name = 42; /* triggers the getter */ var x = props.name; /* "42" */ x </code></pre> <p>“Edit 2” –</p> <pre><code>var someObjs = [{a:"",someProp:"b"}]; (function (a) { return someObjs[a]})(0).someProp = "c"; console.log(someObjs[0].someProp); </code></pre> <p>– works because the return value of the function is used LHS, but as part of <em>another</em> expression that evaluates to a <em>property access</em> (using the dot property accessor syntax). The function returns <em>a reference to an object</em> (to an <code>Object</code> instance) whose property is then accessed. The property access being the LHS of an assignment expression, the RHS value (<code>"c"</code>) is assigned to that property.</p> <p>It is functionally equivalent to</p> <pre><code>var someObjs = [ {a: "", someProp: "b"} ]; (function () { return someObjs[0]; }()).someProp = "c"; console.log(someObjs[0].someProp); </code></pre> <p>and therefore to</p> <pre><code>someObjs[0].someProp = "c"; console.log(someObjs[0].someProp); </code></pre> <p>(as you can see, good code style helps a lot with understanding code)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload