Prev: Dynamic iframe caching problem workaround needed for Firefox
Next: JavaScript code mangler / scrambler / ... khm, more than obfuscator... :)
From: Dmitry A. Soshnikov on 22 Dec 2009 06:48 On Dec 22, 2:52 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: > > Statements: > * Use of == where === should be used > if(typeof val === "undefined"){ } > typeof returns always string, so there's no any difference using == or === in this case (algorithms are equivalent: word for word); == is less on one symbol, you can use typeof val == 'undefined'. /ds
From: Dmitry A. Soshnikov on 22 Dec 2009 16:16 On Dec 22, 10:28 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > Dmitry A. Soshnikov wrote: > > > Statements: > > > * Use of == where === should be used > > > if(typeof val === "undefined"){ } > > > typeof returns always string, so there's no any difference using == or > > === in this case (algorithms are equivalent: word for word); == is > > less on one symbol, you can use typeof val == 'undefined'. > > Yes, here you are absolutely right. But `==' can be harmful especially > when `EqualityExpression` and `RelationalExpression` ares from > different type, because one of them will be converted to primitive > value. > > | 11.9.3 The Abstract Equality Comparison Algorithm > > | 1. If Type(x) is different from Type(y), go to step 14. > | [...] > | 21. If Type(x) is Object and Type(y) is either String or Number, > | return the result of the comparison ToPrimitive(x) == y. > > ToPrimitive for `object` call internal [[DefaultValue]] of passed > `object'. If `object' properties `toString' and `valueOf' is not a > objects [[DefaultValue]] throw TypeError. > > e.g. > > var o = {toString : null, valueOf : null}; > try { > o == '';}catch(e) > > { > window.alert(e instanceof TypeError); > > } > > Thanks, I know about that. But I mentioned about exactly typeof operator and string 'undefined' (of whatevery) but not about o == '' ;) And from this point of view == would be enough; === in this case is obsolete. /ds
From: Asen Bozhilov on 22 Dec 2009 14:28 Dmitry A. Soshnikov wrote: > > Statements: > > * Use of == where === should be used > > if(typeof val === "undefined"){ } > > typeof returns always string, so there's no any difference using == or > === in this case (algorithms are equivalent: word for word); == is > less on one symbol, you can use typeof val == 'undefined'. Yes, here you are absolutely right. But `==' can be harmful especially when `EqualityExpression` and `RelationalExpression` ares from different type, because one of them will be converted to primitive value. | 11.9.3 The Abstract Equality Comparison Algorithm | 1. If Type(x) is different from Type(y), go to step 14. | [...] | 21. If Type(x) is Object and Type(y) is either String or Number, | return the result of the comparison ToPrimitive(x) == y. ToPrimitive for `object` call internal [[DefaultValue]] of passed `object'. If `object' properties `toString' and `valueOf' is not a objects [[DefaultValue]] throw TypeError. e.g. var o = {toString : null, valueOf : null}; try { o == ''; }catch(e) { window.alert(e instanceof TypeError); }
From: Asen Bozhilov on 22 Dec 2009 17:20 Asen Bozhilov wrote: > Garrett Smith wrote: > > * Boolean conversion of Host object (sometimes Error-prone) > > //Boolean conversation host object in JScript > var xhr = new ActiveXObject('Microsoft.XMLHTTP'); > Boolean(xhr.open); Garrett do you have any others example where Boolean conversion throw Error? In my example error will be throwing from internal [[Get]] method of `object' referred from `xhr'. var xhr = new ActiveXObject('Microsoft.XMLHTTP'); try { var a = xhr.open; }catch(e) { window.alert(e); //Object doesn't support this property or method } Here isn't the problem in type conversion. The problem is before that. That example can show it own implementation of [[Get]] and [[Put]] methods from that host object. var xhr = new ActiveXObject('Microsoft.XMLHTTP'); try { xhr.open = null; }catch(e) { if (e instanceof ReferenceError) { window.alert('8.7.2 PutValue(V) throw ReferenceError'); } window.alert(e); //TypeError } Can you show better example, because ECMA3 9.2 ToBoolean explicit say: | The operator ToBoolean converts its argument to a value of | type Boolean according to the following table: | [...] | Object true Abstract for me that mean: function ToBoolean(value) { if (Type(value) === Object) { return true; } } So at the moment i can't see where explicit or implicit conversion of any object Native or Host can produce error.
From: RobG on 22 Dec 2009 22:38
On Dec 22, 9:52 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: > I'm putting together a couple of documents: > > 1) code guidelines > 2) code review guidelines > > The goals are to help make for better code reviews here and to help > debate on assessing javascript code quality. > > I'd like to start with my outline of code guidelines and get some > feedback on it. > > Rich Internet Application Development Code Guildelines (Draft) > > Problems: > > Markup: > * Invalid HTML > * sending XHTML as text/html > * xml prolog (cause problems in IE) > * javascript: pseudo protocol > * Not escaping ETAGO. > Replace: "</" + "script>" > with: "<\/script>"; > Replace: "</td>"; > with: "<\/td>"; I don't understand the issue here, is it specifically with the TD end tag? Or is it more generally with end tags in HTML sent as an XHR response? A suitable alternative may be to omit the closing TD tag altogether (valid in HTML 4.01 and draft HTML 5). <URL: http://www.w3.org/TR/html5/syntax.html#syntax-tag-omission > > CSS: > * invalid css > * classNames that do not have semantic meaning While that comment is OK for class values used for CSS, it should be noted that class names are not intended for CSS only. [...] > RegularExpressions > Be simple, but do not match the wrong thing. Not sure what that means. -- Rob |