JavaScript 疑难汇总

3,937 阅读2分钟

types & grammer

  1. 判断以下结果
var s = 'abc';
s[1] = 'B';

console.log(s);

var l = new String('abc');
l[1] = 'B';
console.log(l);
  1. 如何逆序一个字符串?
  2. 接上,为什么不能直接使用 Array.prototype.reverse.call(s) 逆序字符串?
  3. 判断以下结果,为什么会出现这样的情况,如何做出正确的比较?
0.1 + 0.2 === 0.3;
0.8 - 0.6 === 0.3;
  1. 如何判断一个数值为整数?

  2. 如何判断一个数值为 +0?

  3. 'abc'.toUpperCase() 中 'abc' 作为 primitive value,如何访问 toUpperCase 方法

  4. 判断以下结果

Array.isArray(Array.prototype)
  1. 判断以下结果
Boolean(Boolean(false));
Boolean(document.all);

[] == '';
[3] == 3;
[] == false;
42 == true;
  1. 找出以下代码问题 (TDZ)
var a = 3;
let a;
  1. 找出以下代码问题 (TDZ)
var b = 3;

function foo( a = 42, b = a + b + 5 ) {
    // ..
}

foo()

scope & closures

  1. var a = 2 中,EngineScopeCompiler 做了什么工作
  2. 判断以下结果 (Lexical Scope)
var scope = 'global scope';
function checkscope () {
  var scope = 'local scope';
  function f() {
    return scope; 
  }
  return f;
}
  1. 判断以下结果 (Hoisting)
console.log(a);
var a = 3;
  1. 判断以下结果 (Function First)
var foo = 1;
function foo () {

}
console.log(foo);
  1. 判断以下结果 (IIFE & Function First)
var foo = 1;
(function () {
  foo = 2;
  function foo () {
     
  }

  console.log(foo);
})()

console.log(foo);
  1. 判断以下结果,如何按序输出 (Closure)
for (var i = 0; i < 10; i++) {
  setTimeout(function () {
    console.log(i);
  }, i * 1000)
}

this & object prototypes

  1. 判断以下结果 (Default Binding)
function foo() {
  "use strict";
  console.log( this.a );
}

var a = 2;

foo();
  1. 判断以下结果
"use strict";
var a = 2;

console.log(this);
  1. 判断以下结果 (Strict Mode & Default Binding)
function foo() {
	console.log( this.a );
}

var a = 2;

(function(){
	"use strict";

	foo(); // 2
})();
  1. 判断以下结果 (Hard Binding)
function foo () {
  console.log(this.a);
}

const o1 = { a: 3 };
const o2 = { a: 4 };

foo.bind(o1).bind(o2)();
  1. 如何实现 Function.prototype.bindFunction.prototype.softBind

  2. new 的过程中发生了什么,判断以下结果 (new)

function F () {
  this.a = 3;
  return {
    a: 4;
  }
}

const f = new F();
console.log(f.a);
  1. 什么是 data descriptoraccessor descriptor
  2. 如何访问一个对象的属性与如何对一个对象的属性赋值 ([[Get]] & [[Put]])
  3. 如何遍历一个对象 (?iterator)
  4. 如何实现一个继承 (Object.create & call)
  5. 如何实现 __proto__
  6. 如何实现 Object.create