44个 Javascript 变态题解析 (上\下)

LorEmbley 8年前
   <p>让我们一起来看看这些变态题到底变态不变态吧!</p>    <h2>第1题</h2>    <pre>  <code class="language-javascript">["1", "2", "3"].map(parseInt)  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4958876160907137367" rel="nofollow,noindex">Array/map</a></li>     <li><a href="/misc/goto?guid=4959674389044765328" rel="nofollow,noindex">Number/parseInt</a></li>     <li><a href="/misc/goto?guid=4959674389134536332" rel="nofollow,noindex">JavaScript parseInt</a></li>    </ul>    <p>首先, map接受两个参数, 一个回调函数 callback, 一个回调函数的this值</p>    <p>其中回调函数接受三个参数 currentValue, index, arrary;</p>    <p>而题目中, map只传入了回调函数--parseInt.</p>    <p>其次, parseInt 只接受两个两个参数 string, radix(基数).</p>    <p>可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间。</p>    <p>如果省略该参数或其值为 0,则数字将以 10 为基础来解析。如果它以 “0x” 或 “0X” 开头,将以 16 为基数。</p>    <p>如果该参数小于 2 或者大于 36,则 parseInt() 将返回 NaN。</p>    <p>所以本题即问</p>    <pre>  <code class="language-javascript">parseInt('1', 0);  parseInt('2', 1);  parseInt('3', 2);  </code></pre>    <p>首先后两者参数不合法.</p>    <p>所以答案是 [1, NaN, NaN]</p>    <h2>第2题</h2>    <pre>  <code class="language-javascript">[typeof null, null instanceof Object]  </code></pre>    <p>两个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674389213736913" rel="nofollow,noindex">Operators/typeof</a></li>     <li><a href="/misc/goto?guid=4959674389291659291" rel="nofollow,noindex">Operators/instanceof</a></li>     <li><a href="/misc/goto?guid=4959638273473571699" rel="nofollow,noindex">Operators/instanceof(中)</a></li>    </ul>    <p>typeof 返回一个表示类型的字符串.</p>    <p>instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上.</p>    <p>这个题可以直接看链接... 因为 typeof null === 'object' 自语言之初就是这样....</p>    <p>typeof 的结果请看下表:</p>    <pre>  <code class="language-javascript">type         result  Undefined   "undefined"  Null        "object"  Boolean     "boolean"  Number      "number"  String      "string"  Symbol      "symbol"  Host object Implementation-dependent  Function    "function"  Object      "object"  </code></pre>    <p>所以答案 [object, false]</p>    <h2>第3题</h2>    <pre>  <code class="language-javascript">[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959637675011269324" rel="nofollow,noindex">Array/Reduce</a></li>    </ul>    <p>arr.reduce(callback[, initialValue])</p>    <p>reduce接受两个参数, 一个回调, 一个初始值.</p>    <p>回调函数接受四个参数 previousValue, currentValue, currentIndex, array</p>    <p>需要注意的是 If the array is empty and no initialValue was provided, TypeError would be thrown.</p>    <p>所以第二个表达式会报异常. 第一个表达式等价于 Math.pow(3, 2) => 9; Math.pow(9, 1) =>9</p>    <p>答案 an error</p>    <h2>第4题</h2>    <pre>  <code class="language-javascript">var val = 'smtg';  console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing');  </code></pre>    <p>两个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674389431509117" rel="nofollow,noindex">Operators/Operator_Precedence</a></li>     <li><a href="/misc/goto?guid=4959674389520821148" rel="nofollow,noindex">Operators/Conditional_Operator</a></li>    </ul>    <p>简而言之 + 的优先级 大于  ?</p>    <p>所以原题等价于 'Value is true' ? 'Somthing' : 'Nonthing' 而不是  'Value is' + (true ? 'Something' : 'Nonthing')</p>    <p>答案 'Something'</p>    <h2>第5题</h2>    <pre>  <code class="language-javascript">var name = 'World!';  (function () {      if (typeof name === 'undefined') {          var name = 'Jack';          console.log('Goodbye ' + name);      } else {          console.log('Hello ' + name);      }  })();  </code></pre>    <p>这个相对简单, 一个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674389602271078" rel="nofollow,noindex">Hoisting</a></li>    </ul>    <p>在 JavaScript中, functions 和 variables 会被提升。变量提升是JavaScript将声明移至作用域 scope (全局域或者当前函数作用域) 顶部的行为。</p>    <p>这个题目相当于</p>    <pre>  <code class="language-javascript">var name = 'World!';  (function () {      var name;      if (typeof name === 'undefined') {          name = 'Jack';          console.log('Goodbye ' + name);      } else {          console.log('Hello ' + name);      }  })();  </code></pre>    <p>所以答案是 'Goodbye Jack'</p>    <h2>第6题</h2>    <pre>  <code class="language-javascript">var END = Math.pow(2, 53);  var START = END - 100;  var count = 0;  for (var i = START; i <= END; i++) {      count++;  }  console.log(count);  </code></pre>    <p>一个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959670158191228642" rel="nofollow,noindex">Infinity</a></li>    </ul>    <p>在 JS 里, Math.pow(2, 53) == 9007199254740992 是可以表示的最大值. 最大值加一还是最大值. 所以循环不会停.</p>    <h2>第7题</h2>    <pre>  <code class="language-javascript">var ary = [0,1,2];  ary[10] = 10;  ary.filter(function(x) { return x === undefined;});  </code></pre>    <p>答案是 []</p>    <p>看一篇文章理解稀疏数组</p>    <ul>     <li><a href="/misc/goto?guid=4959674389712477016" rel="nofollow,noindex">译 JavaScript中的稀疏数组与密集数组</a></li>     <li><a href="/misc/goto?guid=4958876161018346773" rel="nofollow,noindex">Array/filter</a></li>    </ul>    <p>我们来看一下 Array.prototype.filter 的 polyfill:</p>    <pre>  <code class="language-javascript">if (!Array.prototype.filter) {    Array.prototype.filter = function(fun/*, thisArg*/) {      'use strict';        if (this === void 0 || this === null) {        throw new TypeError();      }        var t = Object(this);      var len = t.length >>> 0;      if (typeof fun !== 'function') {        throw new TypeError();      }        var res = [];      var thisArg = arguments.length >= 2 ? arguments[1] : void 0;      for (var i = 0; i < len; i++) {        if (i in t) { // 注意这里!!!          var val = t[i];          if (fun.call(thisArg, val, i, t)) {            res.push(val);          }        }      }        return res;    };  }  </code></pre>    <p>我们看到在迭代这个数组的时候, 首先检查了这个索引值是不是数组的一个属性, 那么我们测试一下.</p>    <pre>  <code class="language-javascript">0 in ary; => true  3 in ary; => false  10 in ary; => true  </code></pre>    <p>也就是说 从 3 - 9 都是没有初始化的'坑'!, 这些索引并不存在与数组中. 在 array 的函数调用的时候是会跳过这些'坑'的.</p>    <h2>第8题</h2>    <pre>  <code class="language-javascript">var two   = 0.2  var one   = 0.1  var eight = 0.8  var six   = 0.6  [two - one == one, eight - six == two]  </code></pre>    <ul>     <li><a href="/misc/goto?guid=4959674389818113183" rel="nofollow,noindex">JavaScript的设计缺陷?浮点运算:0.1 + 0.2 != 0.3</a></li>    </ul>    <p>IEEE 754标准中的浮点数并不能精确地表达小数</p>    <p>那什么时候精准, 什么时候不经准呢? 笔者也不知道...</p>    <p>答案 [true, false]</p>    <h2>第9题</h2>    <pre>  <code class="language-javascript">function showCase(value) {      switch(value) {      case 'A':          console.log('Case A');          break;      case 'B':          console.log('Case B');          break;      case undefined:          console.log('undefined');          break;      default:          console.log('Do not know!');      }  }  showCase(new String('A'));  </code></pre>    <p>两个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674389899005755" rel="nofollow,noindex">Statements/switch</a></li>     <li><a href="/misc/goto?guid=4959674389988273827" rel="nofollow,noindex">String</a></li>    </ul>    <p>switch 是严格比较, String 实例和 字符串不一样.</p>    <pre>  <code class="language-javascript">var s_prim = 'foo';  var s_obj = new String(s_prim);    console.log(typeof s_prim); // "string"  console.log(typeof s_obj);  // "object"  console.log(s_prim === s_obj); // false    </code></pre>    <p>答案是 'Do not know!'</p>    <h2>第10题</h2>    <pre>  <code class="language-javascript">function showCase2(value) {      switch(value) {      case 'A':          console.log('Case A');          break;      case 'B':          console.log('Case B');          break;      case undefined:          console.log('undefined');          break;      default:          console.log('Do not know!');      }  }  showCase2(String('A'));  </code></pre>    <p>解释:</p>    <p>String(x) does not create an object but does return a string, i.e. typeof String(1) === "string"</p>    <p>还是刚才的知识点, 只不过 String 不仅是个构造函数 直接调用返回一个字符串哦.</p>    <p>答案 'Case A'</p>    <h2>第11题</h2>    <pre>  <code class="language-javascript">function isOdd(num) {      return num % 2 == 1;  }  function isEven(num) {      return num % 2 == 0;  }  function isSane(num) {      return isEven(num) || isOdd(num);  }  var values = [7, 4, '13', -9, Infinity];  values.map(isSane);  </code></pre>    <p>一个知识点</p>    <ul>     <li><a href="/misc/goto?guid=4959674390068322066" rel="nofollow,noindex">Arithmetic_Operators#Remainder</a></li>    </ul>    <p>此题等价于</p>    <pre>  <code class="language-javascript">7 % 2 => 1  4 % 2 => 0  '13' % 2 => 1  -9 % % 2 => -1  Infinity % 2 => NaN  </code></pre>    <p>需要注意的是 余数的正负号随第一个操作数.</p>    <p>答案 [true, true, true, false, false]</p>    <h2>第12题</h2>    <pre>  <code class="language-javascript">parseInt(3, 8)  parseInt(3, 2)  parseInt(3, 0)  </code></pre>    <p>第一个题讲过了, 答案 3, NaN, 3</p>    <h2>第13题</h2>    <pre>  <code class="language-javascript">Array.isArray( Array.prototype )  </code></pre>    <p>一个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959617420843067883" rel="nofollow,noindex">Array/prototype</a></li>    </ul>    <p>一个鲜为人知的实事: Array.prototype => [] ;</p>    <p>答案: true</p>    <h2>第14题</h2>    <pre>  <code class="language-javascript">var a = [0];  if ([0]) {    console.log(a == true);  } else {    console.log("wut");  }  </code></pre>    <ul>     <li><a href="/misc/goto?guid=4958865325259308539" rel="nofollow,noindex">JavaScript-Equality-Table</a></li>    </ul>    <p>答案: false</p>    <h2>第15题</h2>    <pre>  <code class="language-javascript">[]==[]    </code></pre>    <p>== 是万恶之源, 看上图</p>    <p>答案是 false</p>    <h2>第16题</h2>    <pre>  <code class="language-javascript">'5' + 3  '5' - 3  </code></pre>    <p>两个知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390203240434" rel="nofollow,noindex">Arithmetic_Operators#Addition</a></li>     <li><a href="/misc/goto?guid=4959674390290009581" rel="nofollow,noindex">Arithmetic_Operators#Subtraction</a></li>    </ul>    <p>+ 用来表示两个数的和或者字符串拼接,  - 表示两数之差.</p>    <p>请看例子, 体会区别:</p>    <pre>  <code class="language-javascript">> '5' + 3  '53'  > 5 + '3'  '53'  > 5 - '3'  2  > '5' - 3  2  > '5' - '3'  2  </code></pre>    <p>也就是说 - 会尽可能的将两个操作数变成数字, 而  + 如果两边不都是数字, 那么就是字符串拼接.</p>    <p>答案是 '53', 2</p>    <h2>第17题</h2>    <pre>  <code class="language-javascript">1 + - + + + - + 1    </code></pre>    <p>这里应该是(倒着看)</p>    <pre>  <code class="language-javascript">1 + (a)  => 2  a = - (b) => 1  b = + (c) => -1  c = + (d) => -1  d = + (e) => -1  e = + (f) => -1  f = - (g) => -1  g = + 1   => 1  </code></pre>    <p>所以答案 2</p>    <h2>第18题</h2>    <pre>  <code class="language-javascript">var ary = Array(3);  ary[0]=2  ary.map(function(elem) { return '1'; });  </code></pre>    <p>稀疏数组. 同第7题.</p>    <p>题目中的数组其实是一个长度为3, 但是没有内容的数组, array 上的操作会跳过这些未初始化的'坑'.</p>    <p>所以答案是 ["1", undefined × 2]</p>    <p>这里贴上 Array.prototype.map 的 polyfill.</p>    <pre>  <code class="language-javascript">Array.prototype.map = function(callback, thisArg) {            var T, A, k;            if (this == null) {              throw new TypeError(' this is null or not defined');          }            var O = Object(this);          var len = O.length >>> 0;          if (typeof callback !== 'function') {              throw new TypeError(callback + ' is not a function');          }          if (arguments.length > 1) {              T = thisArg;          }          A = new Array(len);          k = 0;          while (k < len) {              var kValue, mappedValue;              if (k in O) {                  kValue = O[k];                  mappedValue = callback.call(T, kValue, k, O);                  A[k] = mappedValue;              }              k++;          }          return A;      };  </code></pre>    <h2>第19题</h2>    <pre>  <code class="language-javascript">function sidEffecting(ary) {    ary[0] = ary[2];  }  function bar(a,b,c) {    c = 10    sidEffecting(arguments);    return a + b + c;  }  bar(1,1,1)    </code></pre>    <p>这是一个大坑, 尤其是涉及到 ES6语法的时候</p>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390370834887" rel="nofollow,noindex">Functions/arguments</a></li>    </ul>    <p>首先 The arguments object is an Array-like object corresponding to the arguments passed to a function.</p>    <p>也就是说 arguments 是一个  object , c 就是 arguments[2], 所以对于 c 的修改就是对 arguments[2] 的修改.</p>    <p>所以答案是 21 .</p>    <p>然而!!!!!!</p>    <p>当函数参数涉及到 any rest parameters, any default parameters or any destructured parameters 的时候, 这个 arguments 就不在是一个  mapped arguments object 了.....</p>    <p>请看:</p>    <pre>  <code class="language-javascript">function sidEffecting(ary) {    ary[0] = ary[2];  }  function bar(a,b,c=3) {    c = 10    sidEffecting(arguments);    return a + b + c;  }  bar(1,1,1)  </code></pre>    <p>答案是 12 !!!!</p>    <p>请读者细细体会!!</p>    <h2>第20题</h2>    <pre>  <code class="language-javascript">var a = 111111111111111110000,      b = 1111;  a + b;    </code></pre>    <p>答案还是 111111111111111110000 . 解释是  Lack of precision for numbers in JavaScript affects both small and big numbers. 但是笔者不是很明白................ 请读者赐教!</p>    <h2>第21题</h2>    <pre>  <code class="language-javascript">var x = [].reverse;  x();  </code></pre>    <p>这个题有意思!</p>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390445145271" rel="nofollow,noindex">Array/reverse</a></li>    </ul>    <p>The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.</p>    <p>也就是说 最后会返回这个调用者(this), 可是 x 执行的时候是上下文是全局. 那么最后返回的是 window .</p>    <p>答案是 window</p>    <h2>第22题</h2>    <pre>  <code class="language-javascript">Number.MIN_VALUE > 0  </code></pre>    <p>true</p>    <p>今天先到这里, 下次我们来看后22个题!</p>    <h2>第23题</h2>    <pre>  <code class="language-javascript">[1 < 2 < 3, 3 < 2 < 1]  </code></pre>    <p>这个题也还可以.</p>    <p>这个题会让人误以为是 2 > 1 && 2 < 3 其实不是的.</p>    <p>这个题等价于</p>    <pre>  <code class="language-javascript"> 1 < 2 => true;   true < 3 =>  1 < 3 => true;   3 < 2 => false;   false < 1 => 0 < 1 => true;  </code></pre>    <p>答案是 [true, true]</p>    <h2>第24题</h2>    <pre>  <code class="language-javascript">// the most classic wtf  2 == [[[2]]]  </code></pre>    <p>这个题我是猜的. 我猜的 true , 至于为什么.....</p>    <p>both objects get converted to strings and in both cases the resulting string is "2" 我不能信服...</p>    <h2>第25题</h2>    <pre>  <code class="language-javascript">3.toString()  3..toString()  3...toString()  </code></pre>    <p>这个题也挺逗, 我做对了 :) 答案是 error, '3', error</p>    <p>你如果换一个写法就更费解了</p>    <pre>  <code class="language-javascript">var a = 3;  a.toString()  </code></pre>    <p>这个答案就是 '3' ;</p>    <p>为啥呢?</p>    <p>因为在 js 中 1.1 ,  1. ,  .1 都是合法的数字. 那么在解析  3.toString 的时候这个  . 到底是属于这个数字还是函数调用呢? 只能是数字, 因为 3. 合法啊!</p>    <h2>第26题</h2>    <pre>  <code class="language-javascript">(function(){    var x = y = 1;  })();  console.log(y);  console.log(x);  </code></pre>    <p>答案是 1, error</p>    <p>y 被赋值到全局. x 是局部变量. 所以打印 x 的时候会报 ReferenceError</p>    <h2>第27题</h2>    <pre>  <code class="language-javascript">var a = /123/,      b = /123/;  a == b  a === b  </code></pre>    <p>即使正则的字面量一致, 他们也不相等.</p>    <p>答案 false, false</p>    <h2>第28题</h2>    <pre>  <code class="language-javascript">var a = [1, 2, 3],      b = [1, 2, 3],      c = [1, 2, 4]  a ==  b  a === b  a >   c  a <   c  </code></pre>    <p>字面量相等的数组也不相等.</p>    <p>数组在比较大小的时候按照字典序比较</p>    <p>答案 false, false, false, true</p>    <h2>第29题</h2>    <pre>  <code class="language-javascript">var a = {}, b = Object.prototype;  [a.prototype === b, Object.getPrototypeOf(a) === b]  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390530757335" rel="nofollow,noindex">Object/getPrototypeOf</a></li>    </ul>    <p>只有 Function 拥有一个 prototype 的属性. 所以 a.prototype 为  undefined .</p>    <p>而 Object.getPrototypeOf(obj) 返回一个具体对象的原型(该对象的内部 [[prototype]] 值)</p>    <p>答案 false, true</p>    <h2>第30题</h2>    <pre>  <code class="language-javascript">function f() {}  var a = f.prototype, b = Object.getPrototypeOf(f);  a === b  </code></pre>    <p>f.prototype is the object that will become the parent of any objects created with new f while Object.getPrototypeOf returns the parent in the inheritance hierarchy.</p>    <p>f.prototype 是使用使用 new 创建的 f 实例的原型. 而 Object.getPrototypeOf 是 f 函数的原型.</p>    <p>请看:</p>    <pre>  <code class="language-javascript">a === Object.getPrototypeOf(new f()) // true  b === Function.prototype // true  </code></pre>    <p>答案 false</p>    <h2>31</h2>    <pre>  <code class="language-javascript">function foo() { }  var oldName = foo.name;  foo.name = "bar";  [oldName, foo.name]  </code></pre>    <p>答案 ['foo', 'foo']</p>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4958869726255532053" rel="nofollow,noindex">Function/name</a></li>    </ul>    <p>因为函数的名字不可变.</p>    <h2>第32题</h2>    <pre>  <code class="language-javascript">"1 2 3".replace(/\d/g, parseInt)  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390642794544" rel="nofollow,noindex">String/replace#Specifying_a_function_as_a_parameter</a></li>    </ul>    <p>str.replace(regexp|substr, newSubStr|function)</p>    <p>如果replace函数传入的第二个参数是函数, 那么这个函数将接受如下参数</p>    <ul>     <li>match 首先是匹配的字符串</li>     <li>p1, p2 .... 然后是正则的分组</li>     <li>offset match 匹配的index</li>     <li>string 整个字符串</li>    </ul>    <p>由于题目中的正则没有分组, 所以等价于问</p>    <pre>  <code class="language-javascript">parseInt('1', 0)  parseInt('2', 2)  parseInt('3', 4)  </code></pre>    <p>答案: 1, NaN, 3</p>    <h2>第33题</h2>    <pre>  <code class="language-javascript">function f() {}  var parent = Object.getPrototypeOf(f);  f.name // ?  parent.name // ?  typeof eval(f.name) // ?  typeof eval(parent.name) //  ?  </code></pre>    <p>先说以下答案 'f', 'Empty', 'function', error 这个答案并不重要.....</p>    <p>这里第一小问和第三小问很简单不解释了.</p>    <p>第二小问笔者在自己的浏览器测试的时候是 '' , 第四问是  'undefined'</p>    <p>所以应该是平台相关的. 这里明白 parent === Function.prototype 就好了.</p>    <h2>第34题</h2>    <pre>  <code class="language-javascript">var lowerCaseOnly =  /^[a-z]+$/;  [lowerCaseOnly.test(null), lowerCaseOnly.test()]  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390720896782" rel="nofollow,noindex">RegExp/test</a></li>    </ul>    <p>这里 test 函数会将参数转为字符串. 'nul' ,  'undefined' 自然都是全小写了</p>    <p>答案: true, true</p>    <h2>第35题</h2>    <pre>  <code class="language-javascript">[,,,].join(", ")  </code></pre>    <p>[,,,] =&gt; [undefined &times; 3]</p>    <p>因为javascript 在定义数组的时候允许最后一个元素后跟一个 , , 所以这是个长度为三的稀疏数组(这是长度为三, 并没有 0, 1, 2三个属性哦)</p>    <p>答案: ", , "</p>    <h2>第36题</h2>    <pre>  <code class="language-javascript">var a = {class: "Animal", name: 'Fido'};  a.class  </code></pre>    <p>这个题比较流氓.. 因为是浏览器相关, class 是个保留字(现在是个关键字了)</p>    <p>所以答案不重要, 重要的是自己在取属性名称的时候尽量避免保留字. 如果使用的话请加引号 a['class']</p>    <h2>第37题</h2>    <pre>  <code class="language-javascript">var a = new Date("epoch")  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959617421521832453" rel="nofollow,noindex">Date</a></li>     <li><a href="/misc/goto?guid=4959674390830199687" rel="nofollow,noindex">Date/parse</a></li>    </ul>    <p>简单来说, 如果调用 Date 的构造函数传入一个字符串的话需要符合规范, 即满足 Date.parse 的条件.</p>    <p>另外需要注意的是 如果格式错误 构造函数返回的仍是一个Date 的实例 Invalid Date .</p>    <p>答案 Invalid Date</p>    <h2>第38题</h2>    <pre>  <code class="language-javascript">var a = Function.length,      b = new Function().length  a === b  </code></pre>    <p>我们知道一个function(Function 的实例)的 length 属性就是函数签名的参数个数, 所以 b.length == 0.</p>    <p>另外 Function.length 定义为1......</p>    <p>所以不相等.......答案 false</p>    <h2>第39题</h2>    <pre>  <code class="language-javascript">var a = Date(0);  var b = new Date(0);  var c = new Date();  [a === b, b === c, a === c]  </code></pre>    <p>还是关于Date 的题, 需要注意的是</p>    <ul>     <li>如果不传参数等价于当前时间.</li>     <li>如果是函数调用 返回一个字符串.</li>    </ul>    <p>答案 false, false, false</p>    <h2>第40题</h2>    <pre>  <code class="language-javascript">var min = Math.min(), max = Math.max()  min < max  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959670158438596260" rel="nofollow,noindex">Math/min</a></li>     <li><a href="/misc/goto?guid=4959670158351233097" rel="nofollow,noindex">Math/max</a></li>    </ul>    <p>有趣的是, Math.min 不传参数返回 Infinity , Math.max 不传参数返回  -Infinity </p>    <p>答案: false</p>    <h2>第41题</h2>    <pre>  <code class="language-javascript">function captureOne(re, str) {    var match = re.exec(str);    return match && match[1];  }  var numRe  = /num=(\d+)/ig,      wordRe = /word=(\w+)/i,      a1 = captureOne(numRe,  "num=1"),      a2 = captureOne(wordRe, "word=1"),      a3 = captureOne(numRe,  "NUM=2"),      a4 = captureOne(wordRe,  "WORD=2");  [a1 === a2, a3 === a4]  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4959674390972739411" rel="nofollow,noindex">RegExp/exec</a></li>    </ul>    <p>通俗的讲</p>    <p>因为第一个正则有一个 g 选项 它会‘记忆’他所匹配的内容, 等匹配后他会从上次匹配的索引继续, 而第二个正则不会</p>    <p>举个例子</p>    <pre>  <code class="language-javascript">var myRe = /ab*/g;  var str = 'abbcdefabh';  var myArray;  while ((myArray = myRe.exec(str)) !== null) {    var msg = 'Found ' + myArray[0] + '. ';    msg += 'Next match starts at ' + myRe.lastIndex;    console.log(msg);  }  // Found abb. Next match starts at 3  // Found ab. Next match starts at 9  </code></pre>    <p>所以 a1 = '1'; a2 = '1'; a3 = null; a4 = '2'</p>    <p>答案 [true, false]</p>    <h2>第42题</h2>    <pre>  <code class="language-javascript">var a = new Date("2014-03-19"),      b = new Date(2014, 03, 19);  [a.getDay() === b.getDay(), a.getMonth() === b.getMonth()]    </code></pre>    <p>这个....</p>    <p>JavaScript inherits 40 years old design from C: days are 1-indexed in C's struct tm, but months are 0 indexed. In addition to that, getDay returns the 0-indexed day of the week, to get the 1-indexed day of the month you have to use getDate, which doesn't return a Date object.</p>    <pre>  <code class="language-javascript">a.getDay()  3  b.getDay()  6  a.getMonth()  2  b.getMonth()  3  </code></pre>    <p>都是套路!</p>    <p>答案 [false, false]</p>    <h2>第43题</h2>    <pre>  <code class="language-javascript">if ('http://giftwrapped.com/picture.jpg'.match('.gif')) {    'a gif file'  } else {    'not a gif file'  }  </code></pre>    <p>知识点:</p>    <ul>     <li><a href="/misc/goto?guid=4958827865154204809" rel="nofollow,noindex">String/match</a></li>    </ul>    <p>String.prototype.match 接受一个正则, 如果不是, 按照 new RegExp(obj) 转化. 所以  . 并不会转义</p>    <p>那么  /gif 就匹配了 /.gif/</p>    <p>答案: 'a gif file'</p>    <h2>第44题</h2>    <pre>  <code class="language-javascript">function foo(a) {      var a;      return a;  }  function bar(a) {      var a = 'bye';      return a;  }  [foo('hello'), bar('hello')]  </code></pre>    <p>在两个函数里, a作为参数其实已经声明了, 所以 var a; var a = 'bye' 其实就是  a; a ='bye'</p>    <p>所以答案 'hello', 'bye'</p>    <p>全部结束!</p>    <h2>总结</h2>    <p>由于笔者水平有限, 如果解释有误, 还望指出 </p>    <p>通过整理, 笔者发现绝大部分题目都是因为自己对于基础知识或者说某个 API 的参数理解偏差才做错的.</p>    <p>笔者的重灾区在原型那一块, 所以这次被虐和整理还是很有意义呀.</p>    <p>笔者相信 坚实的基础是深入编程的前提. 所以基础书还是要常看啊 </p>    <p>最后这些变态题现在看看还变态嘛?</p>    <p> </p>    <p>来自: <a href="/misc/goto?guid=4959674391166851712" rel="nofollow">http://ourjs.com/detail/5761040488feaf2d031d2526</a></p>    <p> </p>