日期:2014-05-16  浏览次数:20547 次

测试你的JavaScript能力?

测试你的JavaScript能力?
2010年09月07日
  今看到一篇国外测试JS的题目,做了下,结果让人汗颜! 看来JS还有好多要学...... 地址:http://perfectionkills.com/javascript-quiz/
  司徒正美的答案解析:http://www.cnblogs.com/rubylouvre/archive/2010/02/ 13/1667565.html
  1. (function(){ return typeof arguments; })();   答案: object
  arguments是一个类似数组但不是数组的对象,说它类似(仅仅是类似)数组是因为其具有数组一样的访问性质及方式,可以由 arguments[n]来访问对应的单个参数的值,并拥有数组长度属性length。还有就是arguments对象存储的是实际传递给函数的参数,而不局限于函数声明所定义的参数列表(用funcName.length取),而且不能显式创建 arguments 对象。arguments 对象只有函数开始时才可用。
  http://www.cnblogs.com/sharplife/archive/2006/12/3 1/608941.html
  2.  var f = function g(){ return 23; }; typeof g(); 答案: erro
  3. (function(x){ delete x; return x; })(1); 答案: 1
  delete expression  从对象中删除一个属性,或从数组中删除一个元素。
  expression 参数是一个有效的 JScript 表达式,通常是一个属性名或数组元素。 
  说明
  如果 expression 的结果是一个对象,且在 expression 中指定的属性存在,而该对象又不允许它被删除,则返回 false。在所有其他情况下,返回 true。
  4. var y = 1, x = y = typeof x; x; 答案: undefined 
  5. (function f(f){ return typeof f(); })(function(){ return 1; }); 答案: number
  函数名被优先级更高的参数名覆盖了
  This function calls itself f - but it also calls its first argument f. Who wins? The argument does. 'nuff said. It's important to understand that this is not a closure variable. Instead, it is determined at function-run-time, by the entity which it 'belongs to' at the time - and if there is no such object, then the object is window. So if you call foo.bar(), then this will point at foo. But when you call arguments[0](), it points at arguments instead. Since arguments.baz is undefined, you get this particular behaviour. Since the assignment to global variable f makes this function not belong to any entity, this points to window and the result is again "undefined" - unless you habitually set window.baz. 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