angular-cn/public/docs/js/latest/api/annotations/Class-function.jade

99 lines
1.8 KiB
Plaintext

.l-main-section
h2(class="function export") Class
pre.prettyprint
code.
Class(clsDef: ClassDefinition) : Type
p.location-badge.
exported from <a href='../annotations'>angular2/annotations</a>
defined in <a href="https://github.com/angular/angular/tree/2.0.0-alpha.32/modules/angular2/src/util/decorators.ts#L124-L231">angular2/src/util/decorators.ts (line 124)</a>
:markdown
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 = neg.Class({
constructor: [String, [new Query(), QueryList], function(name, queryList) {
...
}];
});
```
is equivalent to ES6:
```
class MyService {
constructor(name: string, @Query() queryList: QueryList) {
...
}
}
```
## 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;
}
});
```