Prev: FAQ Topic - How do I prompt a "Save As" dialog for an accepted mime type? (2009-12-31)
Next: SproutCore--over 20000 lines of new code!
From: Asen Bozhilov on 30 Dec 2009 20:36 Before few weeks ago Garret Smith payed attention for "Conditional statement with an assignment expression": <URL:http://groups.google.bg/group/comp.lang.javascript/msg/ 9299befb874eb9a0> He maintain theory for readable of code in statements like this one: var x; if (x = function(){}) { x(); } This is *complete* valid and *safe* approach: | 11.13.1 Simple Assignment ( = ) | The production AssignmentExpression : | LeftHandSideExpression = AssignmentExpression is evaluated as follows: | 1. Evaluate LeftHandSideExpression. | 2. Evaluate AssignmentExpression. | 3. Call GetValue(Result(2)). | 4. Call PutValue(Result(1), Result(3)). | 5. Return Result(3). Step 4 return `undefined` independent from that is putted value or not. `Simple Assignment` returned value from step 3. All right but if that property for which i try to assign value has {ReadOnly} attribute? if (Object.prototype = function(){}) { try { Object.prototype(); }catch(e) { /*Throwed from step 5 in 11.2.3 Function Calls */ window.alert(e instanceof TypeError); } }
From: David Mark on 30 Dec 2009 20:47 On Dec 30, 8:36 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > Before few weeks ago Garret Smith payed attention for "Conditional > statement with an assignment expression": > <URL:http://groups.google.bg/group/comp.lang.javascript/msg/ > 9299befb874eb9a0> > > He maintain theory for readable of code in statements like this one: > > var x; > if (x = function(){}) > { > x(); > > } > > This is *complete* valid and *safe* approach: Sure, it is technically sound. But it is considered bad form (style) in that it can be mistaken for a test rather than an assignment. You have to consider the programmer that comes next.
From: Asen Bozhilov on 30 Dec 2009 20:59 David Mark wrote: > Sure, it is technically sound. But it is considered bad form (style) > in that it can be mistaken for a test rather than an assignment. You > have to consider the programmer that comes next. Definitely. In other hands, that is the feature of language and it is complete valid. You write program for competent people, which knows what doing with tools in their hand. However, at all that is bad practice, but there is not only problem with readability of code. The main goal of that thread is exactly that. I just catch that and i share with members of c.l.js. Why internal `PutValue' doesn't return anything in explicit way? Why `Simple Assignment` ever return value from step 3?
From: David Mark on 30 Dec 2009 21:20 On Dec 30, 8:59 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > David Mark wrote: > > Sure, it is technically sound. But it is considered bad form (style) > > in that it can be mistaken for a test rather than an assignment. You > > have to consider the programmer that comes next. > > Definitely. In other hands, that is the feature of language and it is > complete valid. You write program for competent people, which knows > what doing with tools in their hand. Sure. I mean that it could be momentarily mistaken, requiring the developer to pause and consider if it is a typo. > However, at all that is bad > practice, but there is not only problem with readability of code. The > main goal of that thread is exactly that. I just catch that and i > share with members of c.l.js. Your efforts are appreciated. > Why internal `PutValue' doesn't return anything in explicit way? The specs are what they are. :) > Why`Simple Assignment` ever return value from step 3? Consider this "lazy" pattern:- var myFunction = function() { var fn; // Scene missing--determine what function to use return (myFunction = fn)(); // Replace and call }; Of course, that last line is also considered bad form in some circles (I'm neutral on that one). IIRC, JSLint considers it a "bad invocation".
From: Dmitry A. Soshnikov on 31 Dec 2009 06:14
On Dec 31, 4:59 am, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: [snip] > > Why internal `PutValue' doesn't return anything in explicit way? Why > `Simple Assignment` ever return value from step 3? It can be useful in `return' statement. For example, memoization as an optimization technique: function getSomething() { return something || (something = getSomethingWithHugeCalculation()); } Sure this snippet can be replaced (and with more readability for some) with the following: function getSomething() { if (!somethig) { something = getSomethingWithHugeCalculation(); } return something; } But I'm mentioning this just as example where it can take place. In Ruby e.g. there's a special syntactic sugar for that (which I'd like to see in ES too): ||= def get_something something ||= get_something_with_huge_calculation end Regarding exactly your case, there's no difference whether you assign before `if' statement of inside its brackets. But, yep, psychological this assignment inside the if's brackets can be treated as successful result of assignment (which is should be Boolean in such thinking). For do no make mistake programmer should know how `if' and assignment work in ES. Whether to use assignment inside the if's brackets or not - well, it depends on complexity of the case (e.g. when you use foo = function () {} - nobody should think that this is comparison). But yep, it's can be hard to read other programmers, especially for those which not well understand how it works. /ds |