日期:2014-05-16 浏览次数:20637 次
The operator , is like the keyword function - it has its fingers in many pies. Usually, commas are used as a syntactic element in var statements, function arguments, and object/array notations. But the humble comma is also an operator in its own right - a left-associative operator which returns the value on its right. (By virtue of being lower-precedence than =, comma is the lowest-precedence operator, as well.) As we now know, the g() syntax just establishes something which the function calls itself - but the function is anonymous to everyone else until assigned a variable. Thus, the second function here is selected by the comma, and it is immediately run. This makes the assignment equivalent to var f = 2;. They don't actually let you answer the first thing that came to my mind, which was "ReferenceError." It turns out that typeof x doesn't generate ReferenceErrors when x is not a valid variable in the local codespace. In any case, the above comments about function operators not being function statements - and thus not defining a variable f in the local scope - causes f to be an invalid variable within that scope. Thus, its typeof is "undefined". 函数声明只能裸露于全局作用域下或位于函数体中。从句法上讲,它们不能出现在块中,例如不能出现在if、while 或 for 语句中。因为块只能包含语句,因此if()中的f函数不能当做函数声明,当成表达式使用可能在预编译阶段做了如下处理 if((XXX = function(){}))因此我们是找不到f的 This is easier than it has to be: typeof always returns strings. Essentially, we're asked to evaluate typeof typeof typeof y - which, rather than throwing a ReferenceError, gives "string". If the argument to the function were {bar: 1}, then this would return "number". However, the argument is {foo: {bar: 1}}, and thus the bar on the root object is undefined. The inner two usages of function are statements which begin with the keyword function - hence they are function statements, not function operators. Function statements float towards the top of their codespace. This is sometimes called "hoisting". Unlike the var statement, which floats the variable declarations but not assignments toward the top of the codespace, the wh