首页 > 代码库 > es65 跨模块常量

es65 跨模块常量

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>跨模块常量</title>
<script src="../../../vendor/traceur.js"></script>
<script src="../../../vendor/bootstrap.js"></script>
<script type="text/traceur">
    //    module.js
    export const intVariantName = 100;
    export const FloatVariantName = 3.14159165;
    export const charVariantName = "variantValue";

    //    use.js,全部引用
    import * as variant from ./module;
    console.log(variant.intVariantName);    //100
    console.log(variant.FloatVariantName);    //3.14159165
    console.log(variant.charVariantName);    //variantValue

    //    otherUse.js,部分引用
    import { FloatVariantName, charVariantName } as variant from ./module;
    console.log(variant.FloatVariantName);    //3.14159165
    console.log(variant.charVariantName);    //variantValue

    //    OnlyInt.js
    import intVariantName as variant from ./module;
    console.log(variant.intVariantName);    //100
</script>
</head>
<body>

</body>
</html>

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>全局变量属性</title>
<script src="../../../vendor/traceur.js"></script>
<script src="../../../vendor/bootstrap.js"></script>
<script type="text/traceur">
/*
 全局对象是最顶层的对象,在浏览器环境指的是window对象,在Node.js指的是global对象。在JavaScript语言中,所有全局变量都是全局对象的属性。(Node的情况比较特殊,这一条只对REPL环境适用,模块环境必须显式声明成global的属性。)
 ES6规定,var命令和function命令声明的全局变量,属于全局对象的属性;let命令、const命令、class命令声明的全局变量,不属于全局对象的属性。

*/
    var varName = "varValue";
    //    浏览器环境下
    console.log(window.varName);    //varValue
    //    Node.js环境下
    console.log(global.varName);    //varValue
    //    通用环境
    console.log(this.varName);        //varValue

    let letName = "letValue";
    console.log(window.letName);    //undefined -- use strict
    console.log(this.letName);        //undefined -- use strict
</script>
</head>
<body>

</body>
</html>

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- 加载Traceur编译器 -->
<script src="http://google.github.io/traceur-compiler/bin/traceur.js" type="text/javascript"></script>
<!-- 将Traceur编译器用于网页 -->
<script src="http://google.github.io/traceur-compiler/src/bootstrap.js" type="text/javascript"></script>
<!-- 打开实验选项,否则有些特性可能编译不成功 -->
<script>
    traceur.options.experimental = true;
</script>
<script type="module">
class Calc {
    constructor(){
        console.log(Calc constructor);
    }
    add(a, b){
        return a + b;
    }
}
var c = new Calc();
console.log(c.add(4,5));
</script>
</head>
<body>

</body>
</html>

 

es65 跨模块常量