常用于约束一个函数及其函数的内外部属性:
interface ICounter {(start: number): void,value: number,interval: number,add(): void,reset(): number,}function generateCounter(): ICounter {let originValue = 0;let counter = (function (start) {originValue = start;counter.reset();}) as ICounter;counter.add = function () {counter.value += counter.interval;}counter.reset = function () {counter.interval = 1;return counter.value = originValue;}return counter;}const myCounter: ICounter = generateCounter()console.log(myCounter)/**[Function: counter] {add: [Function (anonymous)],reset: [Function (anonymous)]}*/myCounter(0)console.log(myCounter)/**[Function: counter] {add: [Function (anonymous)],reset: [Function (anonymous)],interval: 1,value: 0}*/myCounter.add()console.log(myCounter)/**[Function: counter] {add: [Function (anonymous)],reset: [Function (anonymous)],interval: 1,value: 1}*/myCounter.interval = 2console.log(myCounter)/**[Function: counter] {add: [Function (anonymous)],reset: [Function (anonymous)],interval: 2,value: 1}*/myCounter.add()console.log(myCounter)/**[Function: counter] {add: [Function (anonymous)],reset: [Function (anonymous)],interval: 2,value: 3}*/myCounter.reset()console.log(myCounter)/**[Function: counter] {add: [Function (anonymous)],reset: [Function (anonymous)],interval: 1,value: 0}*/
