Earlier quoted context omitted.
I am not a PL expert, but is this a strong vs. weak type problem or an implicit conversion problem? My layman’s (simplistic) model is that “weakly typed” languages allow entities to change their type at runtime, strongly typed languages do not. But what we have here is an expression where entities are being implicitly converted into intermediate values, along with operators that do different things based on the value…
[deleted]
Why ++[[]][+[]]+[+[]] = 10 in JavaScript
11–20 of 80 posts
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#12This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.
I don't see how this example justifies your position, though. Nobody will ever write "++[[]][+[]]+[+[]]" to mean "10" in anything but an obfuscated code contest.
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#13I wonder how people come up with this kind of code to begin with?
This comes from an attempt to write valid JavaScript without using any letters or numbers. It's partly for the sake of demonstrating it's possible and part for security testing reasons, to show that poorly written input validation filters can be bypassed.
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#14Does this trick work in Brendan Eich's first JS implementaion in Netscape 2.0? Which came first, the spec or the implementation?
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#15Earlier quoted context omitted.
This comes from an attempt to write valid JavaScript without using any letters or numbers. It's partly for the sake of demonstrating it's possible and part for security testing reasons, to show that poorly written input validation filters can be bypassed.
True! you wont use it. I guess it'll end up being just an Interview Question.
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#16Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#17This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.
I am not a PL expert, but is this a strong vs. weak type problem or an implicit conversion problem? My layman’s (simplistic) model is that “weakly typed” languages allow entities to change their type at runtime, strongly typed languages do not. But what we have here is an expression where entities are being implicitly converted into intermediate values, along with operators that do different things based on the value…
http://en.wikipedia.org/wiki/Weak_typing
Update to your update:
Strong typing does not subsume static typing. Ruby, for example, is dynamic and quite strongly typed. You can't bypass the type system at all, and the only real coercion is to boolean, which follows a very simple rule.
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#18Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#19This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.
Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript
#20From wtfjs.com: ",,," == Array((null,'cool',false,NaN,4)); // true
(null,'cool',false,NaN,4) evaluates to the last expression - 4
Array(4) creates an array with 4 elements.
Array(4).toString() = ",,,"