Class()

Class

Stable Function

Class Export

export Class(clsDef: ClassDefinition) : Type<any>

Provides a way for expressing ES6 classes with parameter annotations in ES5.

Basic Example

var Greeter = ng.Class({
  constructor: function(name) {
    this.name = name;
  },

  greet: function() {
    alert('Hello ' + this.name + '!');
  }
});

is equivalent to ES6:

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

  greet() {
    alert('Hello ' + this.name + '!');
  }
}

or equivalent to ES5:

var Greeter = function (name) {
  this.name = name;
}

Greeter.prototype.greet = function () {
  alert('Hello ' + this.name + '!');
}

Example with parameter annotations

var MyService = ng.Class({
  constructor: [String, [new Optional(), Service], function(name, myService) {
    ...
  }]
});

is equivalent to ES6:

class MyService {
  constructor(name: string, @Optional() myService: Service) {
    ...
  }
}

Example with inheritance

var Shape = ng.Class({
  constructor: (color) {
    this.color = color;
  }
});

var Square = ng.Class({
  extends: Shape,
  constructor: function(color, size) {
    Shape.call(this, color);
    this.size = size;
  }
});

exported from core-index defined in core/src/util/decorators.ts

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/docs/ts/latest/api/core/index/Class-function.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部