技术控

    今日:69| 主题:49211
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] ES proposal: global

[复制链接]
穿高跟鞋俯视 发表于 2016-9-30 17:04:21
69 3

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
The ECMAScript proposal “          global    ” by Jordan Harband is currently at stage 3. It provides a new standard way of accessing the global object.  
  Referring to the global object

  The following are a few popular ways of referring to the global object:
  
       
  • Global variables:
              
    • Global variable          window: is the classic way of referring to the global object. But it doesn’t work in Node.js and in Web Workers.        
    • Global variable          self: is available in Web Workers and browsers in general. But it isn’t supported by Node.js. Some people use          selfto mark code that works in both Web Workers and normal browser contexts.        
    • Global variable          global: is only available in Node.js. Until now!      
            
  •       this:
              
    •           thisin global scope: refers to the global object. The only problem is that Node.js modules and ES6 modules have their own scopes, which means that this approach doesn’t work there.        
    •           thisduring a function call in sloppy mode: If you call a function via a function call (and not a method call), its          thisrefers to the global object in non-strict mode. In strict mode, it is          undefined.        
    •           new Function('return this')(): works in both strict mode and sloppy mode, because the parameters of          new Function()are always evaluated in sloppy mode. There is one important caveat:          eval,          new Function(), etc. are not available if you use CSP (Content Security Policy). That makes this approach unsuited in many cases.      
          
  The proposal

  The ECMAScript proposal standardizes the global variable    globalfor accessing the global object. It also standardizes that the global object must have    Object.prototypein its prototype chain. The following is already true in web browsers today:  
  1. > Object.prototype.isPrototypeOf(window)
  2.     true
复制代码
Best practices

  The global object is now considered a mistake that JavaScript can’t get rid of, due to backward compatibility. It affects performance negatively and is generally a confusing feature.
  ECMAScript 6 moves away from the global object by providing three new ways for declaring variables that don’t create global properties in global scope (as    vardeclarations and function declarations do):  
  
       
  •       letdeclarations   
  •       constdeclarations   
  • Class declarations  
  In other words: all properties of the global object are global variables, but not all global variables are properties of the global object. For example (executed in global scope):
  1. > var foo;
  2.     > 'foo' in window
  3.     true
  4.    
  5.     > let bar;
  6.     > 'bar' in window
  7.     false
复制代码
It is normally preferable to refer to global variables as variables and not as properties of, e.g.,    window. That has always worked on all JavaScript platforms.  
  Furthermore, starting with ES6 (and even before), most JavaScript code lives in modules and will thus never be in global scope.
  Therefore,    globalwill mostly be relevant for polyfills.  
  A polyfill

  The proposal’s author, Jordan Harband, has written    a polyfillfor it.  
  Using it with CommonJS syntax:
  1. var global = require('system.global')();
复制代码
Using it with ES6 module syntax:
  1. import getGlobal from 'system.global';
  2.     const global = getGlobal();
复制代码
The package always uses the “most native” approach available (    globalon Node.js etc.,    windowin normal browser contexts, etc.).  
  More information on the global object
友荐云推荐




上一篇:深入探索C++对象模式读书笔记
下一篇:Segmentation Protection in Intel Processor
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

kaleungng 发表于 2016-10-1 02:20:54
上火,就容易缺经验,过去,我一直缺, 自从学会复制粘贴以后,我的经验小康了 ,一口气拿五点,不费劲儿,复制一次粘 贴无数次,真实惠
回复 支持 反对

使用道具 举报

刘倩 发表于 2016-10-8 11:41:21
老鼠扛刀,满街找猫!
回复 支持 反对

使用道具 举报

不平胸何以平天下 发表于 2016-10-8 22:03:44
阅!谢谢分享
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表