zoukankan      html  css  js  c++  java
  • javascript严格模式

    设立"严格模式"的目的,主要有以下几个:

      1. 消除Javascript语法的一些不合理、不严谨之处,减少一些怪异行为;

      2. 消除代码运行的一些不安全之处,保证代码运行的安全;

      3. 提高编译器效率,增加运行速度;

      4. 为未来新版本的Javascript做好铺垫。

    严格模式下,变量都必须先用var命令声明,然后再使用。

    test_163:/home/exenode/es6 # more mytest.js 
    class Animal{
            constructor(name){
                    this.name = name;
            }
    
            sayName(){
                    console.log('My name is ' + this.name);
            }
    
    }
    
    class Programmer extends Animal{
            constructor(name){
                    super(name);
            }
    
            program(){
                    console.log("I'm coding...");
            }
    
    }
    
    var animal = new Animal('dummy');
    
    wayou = new Programmer('wyou');
    
    animal.sayName();
    wayou.sayName();
    
    wayou.program();
    test_163:/home/exenode/es6 # 
    test_163:/home/exenode/es6 # node mytest.js 
    /home/exenode/es6/mytest.js:1
    (function (exports, require, module, __filename, __dirname) { class Animal{
                                                                  ^^^^^
    
    SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
        at exports.runInThisContext (vm.js:53:16)
        at Module._compile (module.js:414:25)
        at Object.Module._extensions..js (module.js:442:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:313:12)
        at Function.Module.runMain (module.js:467:10)
        at startup (node.js:136:18)
        at node.js:963:3

    修改如下:

    test_163:/home/exenode/es6 # more mytest.js 
    'use strict';
    class Animal{
            constructor(name){
                    this.name = name;
            }
    
            sayName(){
                    console.log('My name is ' + this.name);
            }
    
    }
    
    class Programmer extends Animal{
            constructor(name){
                    super(name);
            }
    
            program(){
                    console.log("I'm coding...");
            }
    
    }
    
    var animal = new Animal('dummy');
    
    var wayou = new Programmer('wyou');
    
    animal.sayName();
    wayou.sayName();
    
    wayou.program();
    test_163:/home/exenode/es6 # 
    test_163:/home/exenode/es6 # node mytest.js 
    My name is dummy
    My name is wyou
    I'm coding...
    test_163:/home/exenode/es6 # 
  • 相关阅读:
    HBase MapReduce 一些 ClassNotFoundException 所缺少的jar包
    Eclipse 连接不上 hadoop 的解决办法
    Log4j 2 配置
    mysql 查询结果显示行号
    js刷新页面方法 -- (转)
    python 错误--UnboundLocalError: local variable '**' referenced before assignment
    Webview 中FaultyInfo代码说明
    HTML中设置超链接字体 & 字体颜色
    apache log 按日期记录 格式 <GOOD>-- (转)
    linux 下 /bin /sbin 的区别 -- (转)
  • 原文地址:https://www.cnblogs.com/z360519549/p/5461939.html
Copyright © 2011-2022 走看看