JS 事件循环,闭包,作用域链题

  • JS的事件循环机制

console.log('one')
setTimeout(function () {
    console.log('two')    
}, 0)
console.log('three')复制代码

输出是

one
three
two复制代码

此题的关键是JS的事件循环机制,对异步的处理操作,JS有句口令:同步优先、异步靠边、回调垫底。

第一个console.log是同步执行,直接输出one

执行完遇到setTimeout回调(垫底执行),把setTimeout丢到回调消息队列等待

然后console.log('three')也是同步执行,直接输出three,同步的消息队列全部执行完后轮到回调队列执行输出two

  • 闭包与作用域相关题目

function test() {
    var n = 4399;
    function add() {
        n++;
        console.log(n);
    }
    return {
        n: n,
        add: add
    }
}
var result = test(); 
var result2 = test(); 
result.add();
result.add(); 
console.log(result.n); 
result2.add(); 
复制代码

输出

4400
4401
4399
4400复制代码

此题的关键在于明白var result = test() 与var result2 = test()此处分别产生了两个闭包,它们之间是独立,互不干扰的

闭包的产生:在A函数中定义了B函数,B函数中引用了A函数中的变量,此时会产生闭包

var result = test()  // 设置result的属性为: { n:4399, add: add() }
var result2 = test()  // 设置result2的属性为: { n:4399, add: add()  }result.add() // 调用了result的add方法,执行n++,所以n = 4399 + 1 输出4400
result.add() // 再次调用了result的add方法,此时n = 4400 所以n = 4400 + 1 输出4401

console.log(result.n)  //这是输出result的属性n值,输出4399,这里是输出对象属性返回值,并无调用闭包

result2.add() // 调用了result2的add方法,此时n = 4399 所以n = 4399 + 1 输出4400

复制代码

 

  • 作用域链的题目

var myObject = {
    foo: "bar",
    func: function() {
        var self = this;
        console.log(this.foo);
	console.log(self.foo);
	(function() {
	    console.log(this.foo);
	    console.log(self.foo);
	}());
    }
};
myObject.func();
复制代码

输出

bar
bar
undefined
bar复制代码

此题关键理解: 方法或函数由谁调用,方法函数内部的this就指向谁

1、func由myObject调用,this指向myObject,输出bar

2、self 指向myObject,输出bar

3、立即执行匿名函数表达式由window调用,this指向window

4、立即执行执行函数在本作用域找不到self,沿着作用域链向上寻找self变量,找到了父函数中指向myObject对象的self


转载于:https://juejin.im/post/5c6ec5bce51d4560a7341d8f

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值