ES6.2--全局变量和跨模块变量

跨模块常量

  1. const 声明的常量只在当前的代码块中有效。如果页面需要设置跨模块的常量
//con.js模块
export const A = 1;
export const B = 2;

//A.js
import * as con from './con.js';
console.log(con.A);
console.log(con.B);

//B.js
import {A,B} from './con.js';
console.log(A);
console.log(B);


//function.js模块
function toDipsWidth(px) {
    return px * screenWidth / defultWidth;

}
//top, bottom, height   使用对象
function toDipsHeight(px) {
  return px * screenHeight / defultHeight;
}
export {
    toDips,
    toDipsWidth,
    toDipsHeight,

};

// c.js模块
import { toDips, toDipsWidth, toDipsHeight } from '../../../common/utils/PixelRatioUtils';
toDips(750)

全局对象属性

  1. ES5中,全局对象的属性和全局变量是等价的
    window.a = 1;
    console.log(a);  // 1

    var b = 2;
    console.log(window.b)  //2
  1. ES6中,var 命令和function 声明的全局变量依旧是全局对象属性,let , const 和class声明的全局变量不属于全局对象的属性。
let b = 1;
console.log(window.b)  // undefined
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • let 和 const 命令 let 命令 块级作用域 const 命令 顶层对象的属性 gl...
    安小明阅读 4,522评论 0 0
  • let 命令 块级作用域 const 命令 顶层对象的属性 global 对象 let 命令 基本用法 ES6 新...
    嘉奇呦_nice阅读 5,527评论 0 2
  • let 命令 块级作用域 const 命令 顶层对象的属性 global 对象 let 命令 基本用法 ES6 新...
    卞卞村长L阅读 3,725评论 0 0
  • 一、ES6简介 ​ 历时将近6年的时间来制定的新 ECMAScript 标准 ECMAScript 6(亦称 ...
    一岁一枯荣_阅读 11,270评论 8 25
  • 在ES5中,变量声明只有var和function以及隐式声明三种,在ES6中则增加了let,const,impor...
    席小白阅读 13,971评论 5 8