Par Félix Billon
/**Objet anonyme**/
var Module = (function () {
var privateMethod = function () {};
return {
publicMethodOne: function () {
// Je peut appeler ma méthode privées
},
publicMethodtwo: function () {
},
publicMethodThree: function () {
}
};
})();
/**Objet local**/
var Module = (function () {
var myObject = {};
var privateMethod = function () {};
myObject.publicMethodOne = function () {
};
return myObject;
})();
var Module = (function () {
var privateMethod = function () {
// privée
};
var someMethod = function () {
// public
};
var anotherMethod = function () {
// public
};
return {
someMethod: someMethod,
anotherMethod: anotherMethod
};
})();
Fichier : IAnimal.ts
module MonModule {
export interface IAnimal {
nbPatte: number;
vole: boolean;
crie(): void;
}
}
Fichier : Hamster.ts
/// <reference path="IAnimal.ts" />
module MonModule {
export class Hamster implements IAnimal{
nbPatte: number;
vole: boolean;
constructor(v) {
this.nbPatte = 4;
this.vole = false;
}
crie(): void {
console.log('OoO');
}
}
}
Fichier : main.ts
/// <reference path="IAnimal.ts" /> /// <reference path="Hamster.ts" /> let hamster: MonModule.IAnimal = new MonModule.Hamster(); hamster.crie();
Fichier : Hamster.ts
/// <reference path="_all.ts" />
module MonModule {
export class Hamster implements IAnimal{
nbPatte: number;
vole: boolean;
constructor(v) {
this.nbPatte = 4;
this.vole = false;
}
crie(): void {
console.log('OoO');
}
}
}
Fichier : _all.ts
/// <reference path="IAnimal.ts" /> /// <reference path="Hamster.ts" />
Fichier : main.ts
/// <reference path="_all.ts" /> let hamster: MonModule.IAnimal = new MonModule.Hamster(); hamster.crie();
Fichier : IAnimal.ts
interface IAnimal {
nbPatte: number;
vole: boolean;
crie(): void;
}
export default IAnimal;
Fichier : Hamster.ts
import IAnimal from './IAnimal.ts'; // OU import IAnimal = require('./IAnimal');
export default class Hamster implements IAnimal{
nbPatte: number;
vole: boolean;
constructor(v) {
this.nbPatte = 4;
this.vole = false;
}
crie(): void {
console.log('OoO');
}
}
}
Fichier : main.ts
import IAnimal from './IAnimal.ts'; // OU import IAnimal = require('./IAnimal');
import Hamster from './Hamster.ts'; // OU import Hamster = require('./Hamster');
let hamster: IAnimal = new Hamster();
hamster.crie();