笔试面试题总结
1,
var a = 0; function A(){ this.a = 1; setTimeout(function(){ this.a = 2; try{ this.b="b"; throw ''; } catch(e){ this.b='bb'; } },0); this.b="bbb"; } var aa = new A(); setTimeout(function(){ console.log(aa.a); console.log(window.a); console.log(aa.b); console.log(window.b)},0);
settimeout的作用域是window,所以结果为1,2,bbb,b但是将代码中的第二个setTimeout去掉之后,结果为1,0,bbb,undefined,原因是settimeout是在页面加载完毕之后才执行。
2.
function foo(x){ x = x || 1; console.log(x); } foo(2);
var obj = { '1':'a', '2':'b', 'length':2, push:Array.prototype.push } obj.push('c');这里的‘1’‘2’仅仅是变量名,不是下标。适用对象字面量方法定义对象时,不管变量名是不是字符串,都会自动转换成字符串。
push
function is intentionally generic; it does not require that its
this value be an Array object. Therefore it can be transferred to other kinds
push
function can be applied successfully to a host object is implementation-dependent.