JavaScript延伸在鳞片6.6

最后编辑了

squ 6.6船用JavaScript的若干扩展 支持。主要描述主要。

让声明

Early JavaScript versions offered the var keyword for variable declarations:

function factorial(n)
{
    var result;
    if (n > 1) {
        var rest = factorial(n - 1);
        result = rest * n;
    } else {
        result = 1;
    }
    return result;
}

The sometimes non-intuitive side-effect of var declarations is their 范围:它们可以在整个函数内访问’s scope. Or 在功能之外的全球范围内。而且 - 更令人惊讶 - 在声明之前可以访问变量 statement. Even though the value will be undefined in that case.

The let keyword does away with those potential problems:

x; // ReferenceError!
let x = 1;
if (x) {
    let x = 2;
    x;   // <-- 2
}
x; // <-- 1

进一步避免变量声明问题 "我们 e strict" 下面描述的指令是值得的。它会抓住案件 accidental variable usage without a var or let宣言。

Const宣言

The const keyword was available before but had a behavior that pre-dates the standardization in ECMAScript. As of Squish 6.6.0 const will have

a) block scope like let and b)导致尝试改变其价值的错误

c; // ReferenceError!
const c = 1;
c = 2;  // TypeError!

持续的声明需要包含初始化程序值, i.e. const c; by itself is not a valid statement.

箭头功能

经典函数声明

function multiply(a, b)
{
    return a * b;
}

print(multiply(3, 7));  // <-- 21

can be expressed in a short-hand form using the => operator:

let multiply = (a, b) => a * b;

print(multiply(3, 7));   // <-- 21

这种符号特别方便地阐明表达 callback functions:

let arr = [ 3, 5 ];

arr.forEach(v => { test.log(v); });  // logs 3 and 5

模板字符串

Tired of assembling strings using the + operator? A new type of 由反向点标记的字符串文字提供比 由单引号包围的经典字符串。

相反,组装常量

const multiLine = "First line\n" +
    "Second line\n" +
    "Third line`;

多线字符串可以表示为单个文字:

const multiLine = `First line
Second line
Third line`;

而不是混合这样的恒定和可变部件

test.log("Found " + itemCount + " menu items");

the ${...} placeholder can embed expressions that will be evaluated on-the-fly:

test.log(`Found ${itemCount} menu items`);

班级

面向对象的JavaScript编程基于概念 功能和原型对象。一个简洁的对象声明 现在可以通过原型,函数和继承 class关键词。

请注意,脚本解释器必须 the declaration of a 第一次使用前的课程。那’S不同的功能可以 即使声明在脚本中稍后遵循。

构造师

A class declaration can contain a special constructor 功能。 It 将在创建对象时调用 new 操作员。那种对象’初始状态可以安全设置 conveniently.

class Animal
{
    constructor(name) { this.name = name; };
}

var dog = new Animal("Rantanplan");
dog.name;  // "Rantanplan"

原型方法

类对象的函数属性(方法)可以用一个定义 不包括典型的速记表示法 function关键词:

class Animal
{
    constructor(name) { this.name = name; };
    jump() { return this.name + " jumps"; }
}

var dog = new Dog("Rantanplan");
dog.jump();  // "Rantanplan jumps"

静态方法

可以通过课程本身宣布方法 static 关键词。他们不需要班级的实例而不是操作 them.

通常,类函数包含实用程序或工厂函数。

class Animal
{
    constructor(name) { this.name = name; };
    static Dog(name)
    {
        let dog = new Animal(name);
        dog.legs = 4;
        return dog;
    }
}

var dog = Animal.Dog("Rantanplan");
dog.legs;  // 4

延期

一旦宣布课程,它可以作为其他类的基础 将继承其所有属性。

class Bird extends Animal
{
    chirp() { return this.name + " chirps" }
};

极好的

The constructor function of a complex class may want to invoke the 基类的构造函数。这是可能的 super()功能。

class Car {
    constructor(brand) {
        this.brand = brand;
    }
}

class Model extends Car {
    constructor(brand, mod) {
        super(brand);
        this.model = mod;
    }
}

Similarly, the super property references methods of the extended class:

class B { greet() { return "hel"; } }
class C extends B { greet() { return super.greet() + "lo"; } }

let c = new C();
c.greet(); // "hello"

类表达

类也可以被声明为表达式的一部分,并因此被视为对待 like values:

let Animal = class { .... };
let animal = new Animal();

严格模式

A strict parsing mode will be enabled if the magic string "我们 e strict" or 'use strict'is found at the top of a script file or 功能。严格的模式有助于检测常见的编程错误 喜欢访问未释录的变量。

例子:

"use strict";

function main() {
    verboseMode = true;  // <-- error: access to undeclared variable
}

目前,将检测以下问题:

模块代码以及类函数都将始终解释 in strict mode.

The magic "我们 e strict" string merely needs to be the first statement 通过方式在文件或功能中。无论是空格和评论都可以 come first.

关于基于脚本的对象映射的注意事项

由于JavaScript模块将在基于严格的模式脚本中进行评估 在依赖于行为的情况下,对象映射可能需要修复 不允许严格的模式。

指数运营商

A short form of the Math.pow(base, exp) exponentiation function is available via the new ** operator. It can also be part of an assignment.

var n = 2 ** 3;  // <--- 8
n **= 3; // <--- 512

少参数catch子句

The classic way of catching a script exception in a try/catch statement includes the identification of the thrown exception - e in the example below:

try {
    f();
} catch (e) {
    test.log("Function f() threw an exception");
}
try {
    f();
} catch {
    test.log("Function f() threw an exception");
}

各种各样的