请选择 进入手机版 | 继续访问电脑版

技术控

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

[其他] ES proposal: global

[复制链接]
穿高跟鞋俯视 发表于 2016-9-30 17:04:21
144 3
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
kaleungng 发表于 2016-10-1 02:20:54
上火,就容易缺经验,过去,我一直缺, 自从学会复制粘贴以后,我的经验小康了 ,一口气拿五点,不费劲儿,复制一次粘 贴无数次,真实惠
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表