1.执行环境判断 window 或 self

window or self ?

在 underscore 的判断所处环境的代码中,似乎我们没有看到 window 对象的引用,其实,在浏览器环境下,self 保存的就是当前 window 对象的引用。那么相比较于使用 window,使用 self 有什么优势呢?我们看到 MDN 上有这么一句话:

The Window.self read-only property returns the window itself, as a WindowProxy. It can be used with dot notation on a window object (that is, window.self) or standalone (self). The advantage of the standalone notation is that a similar notation exists for non-window contexts, such as in Web Workers.

概括来说,就是 self 还能用于一些不具有窗口的上下文环境中,比如 Web Workers。所以,为了服务于更多场景,underscore 选择了更加通用的 self 对象。

!(function(){
    var root = (typeof self == "object" && self.self === self && self) || (typeof global == "object" && global.global === global && global) || this;
});

  

转载于:https://www.cnblogs.com/alantao/p/7831287.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值