What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012? -


the 'wat' talk codemash 2012 points out few bizarre quirks ruby , javascript.

i have made jsfiddle of results @ http://jsfiddle.net/fe479/9/.

the behaviours specific javascript (as don't know ruby) listed below.

i found in jsfiddle of results didn't correspond in video, , not sure why. am, however, curious know how javascript handling working behind scenes in each case.

empty array + empty array [] + [] result: <empty string> 

i quite curious + operator when used arrays in javascript. matches video's result.

empty array + object [] + {} result: [object] 

this matches video's result. what's going on here? why object. + operator do?

object + empty array {} + [] result [object] 

this doesn't match video. video suggests result 0, whereas [object].

object + object {} + {} result: [object][object] 

this doesn't match video either, , how outputting variable result in 2 objects? maybe jsfiddle wrong.

array(16).join("wat" - 1) result: nannannannannannannannannannannannannannannannan 

doing wat + 1 results in wat1wat1wat1wat1...

i suspect straightforward behaviour trying subtract number string results in nan.

here's list of explanations results you're seeing (and supposed seeing). references i'm using ecma-262 standard.

  1. [] + []

    when using addition operator, both left , right operands converted primitives first (§11.6.1). per §9.1, converting object (in case array) primitive returns default value, objects valid tostring() method result of calling object.tostring() (§8.12.8). arrays same calling array.join() (§15.4.4.2). joining empty array results in empty string, step #7 of addition operator returns concatenation of 2 empty strings, empty string.

  2. [] + {}

    similar [] + [], both operands converted primitives first. "object objects" (§15.2), again result of calling object.tostring(), non-null, non-undefined objects "[object object]" (§15.2.4.2).

  3. {} + []

    the {} here not parsed object, instead empty block (§12.1, @ least long you're not forcing statement expression, more later). return value of empty blocks empty, result of statement same +[]. unary + operator (§11.4.6) returns tonumber(toprimitive(operand)). know, toprimitive([]) empty string, , according §9.3.1, tonumber("") 0.

  4. {} + {}

    similar previous case, first {} parsed block empty return value. again, +{} same tonumber(toprimitive({})), , toprimitive({}) "[object object]" (see [] + {}). result of +{}, have apply tonumber on string "[object object]". when following steps §9.3.1, nan result:

    if grammar cannot interpret string expansion of stringnumericliteral, result of tonumber nan.

  5. array(16).join("wat" - 1)

    as per §15.4.1.1 , §15.4.2.2, array(16) creates new array length 16. value of argument join, §11.6.2 steps #5 , #6 show have convert both operands number using tonumber. tonumber(1) 1 (§9.3), whereas tonumber("wat") again nan per §9.3.1. following step 7 of §11.6.2, §11.6.3 dictates

    if either operand nan, result nan.

    so argument array(16).join nan. following §15.4.4.5 (array.prototype.join), have call tostring on argument, "nan" (§9.8.1):

    if m nan, return string "nan".

    following step 10 of §15.4.4.5, 15 repetitions of concatenation of "nan" , empty string, equals result you're seeing. when using "wat" + 1 instead of "wat" - 1 argument, addition operator converts 1 string instead of converting "wat" number, calls array(16).join("wat1").

as why you're seeing different results {} + [] case: when using function argument, you're forcing statement expressionstatement, makes impossible parse {} empty block, it's instead parsed empty object literal.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -