class Statement

Declares a new class.

语法

class classname () [extends object] {  [constructor([arg1 [,... [,argN]]]) {    statements  }]  [[static] method([arg1 [,... [,argN]]]) {    statements  }]}

参数

classname

必要参数。The name of the class.

object

可选参数。An object or class from which the new class inherits properties and methods.

constructor

可选参数。A constructor function that initializes the new class instance.

arg1...argN

可选参数。An optional, comma-separated list of arguments the function understands.

statements

可选参数。One or more JavaScript statements.

static

可选参数。Specifies a static method.

方法

可选参数。One or more JavaScript instance or static methods that can be called on a class instance.

备注

A class allows you to create new objects using prototype-based inheritance, constructors, instance methods, and static methods. You can use the super object within a class constructor or class method to call the same constructor or method in the parent class or object. Optionally, use the extends statement after the class name to specify the class or object from which the new class inherits methods.

示例

class Spelunking extends EXPERIENCE.Outdoor { 
 constructor(name, location) { 
  super(name, location); 
  this.minSkill = Spelunking.defaultSkill(); 
  //... 
 } 
 update(minSkill) { 
  //... 
  super.update(minSkill); 
 } 
 static defaultSkill() { 
  return new EXPERIENCE.Level3(); 
 } 
}

示例

You can also create computed property names for classes. The following code example creates a computed property name using set syntax.

var propName = "prop_42"; 
class Spelunking { 
  set [propName](v) { 
    this.value = v; 
  } 
}; 
var s = new Spelunking(); 
console.log(s.value); 
s.prop_42 = 42; 
console.log(s.value); 
// Output: 
// undefined 
// 42

示例

The following code example creates a property name for a class dynamically using get syntax.

var propName = "prop_42"; 
class Spelunking { 
  get [propName]() { 
    return 777; 
  } 
} 
var s = new Spelunking(); 
console.log(s.prop_42); 
// Output: 
// 777

必备条件

Supported in Microsoft Edge (Edge browser) with experimental JavaScript features enabled (about:flags). 请参阅版本信息

在以下文档模式中不受支持:怪异模式、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式、Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。在 Windows 8.1 中不受支持。

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。