Coffee | Type | Node 编写 Module

使用 Coffee | Type | Node 编写 Module 的方法

Node 编写的对象模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var util = require("util");

function Animal() {}
function Person(name, age) {
this.name = name;
this.age = age;
Animal.call(this);
}
util.inherits(Person, Animal);
Person.prototype.say = function () {
console.log("This Is %s Speaking, I'm %d years old", this.name, this.age);
};

module.exports = {
"Person": Person
"Animal": Animal
};

Coffee 编写的相同功能的模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Animal
constructor: () ->
say: () ->

class Person extends Animal
@name
@age

constructor: (name, age) ->
this.name = name
this.age = age

say: () ->
console.log("This Is %s Speaking, I'm %d years old", this.name, this.age);

module.exports =
Person: Person
Animal: Animal

TypeScript 编写相同功能的模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export class Animal {}

export class Person extends Animal {
private name: string = '';
private age: number = 20;

public constructor(name: string, age: number) {
super();
this.name = name;
this.age = age;
}

public say(): void {
console.log("This Is %s Speaking, I'm %d years old", this.name, this.age);
}
}
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.