美文网首页
ES6.2--全局变量和跨模块变量

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

作者: flyingtoparis | 来源:发表于2017-09-15 23:33 被阅读0次

跨模块常量

  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

相关文章

网友评论

      本文标题:ES6.2--全局变量和跨模块变量

      本文链接:https://www.haomeiwen.com/subject/ruaesxtx.html