面试题记录

笔试面试题总结

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);

输出为2 当两个不为零的数字相或时,结果是前面的那个数字。


3.
var obj = {
    '1':'a',
    '2':'b',
    'length':2,
    push:Array.prototype.push
}
obj.push('c');
这里的‘1’‘2’仅仅是变量名,不是下标。适用对象字面量方法定义对象时,不管变量名是不是字符串,都会自动转换成字符串。
这里把c push进去之后,length的值会变成3.
数组的push方法是在数组长度的位置插入新的元素,那么这里就是在obj[2]的地方插入了c,所以b就被顶替掉了。然后返回的数组长度会自动加1,所以length变为3.
这是es5文档里的
The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call.
元素被添加到数组末尾,新的数组长度会被当作结果返回。
The 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
of objects for use as a method.Whether the push function can be applied successfully to a host object is implementation-dependent.
push方法并不是数组独有的,因此可以被转化成其他种类对象的方法去使用。


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值