面试题答案
一键面试namespace Animals {
export interface Dog {
name: string;
bark(): string;
}
export const myDog: Dog = {
name: 'Buddy',
bark() {
return 'Woof!';
}
};
}
namespace Plants {
export class Tree {
height: number;
constructor(height: number) {
this.height = height;
}
grow() {
// 这里可以添加生长逻辑
}
}
}
// 外部调用
console.log(Animals.myDog.name);
console.log(Animals.myDog.bark());
const myTree = new Plants.Tree(10);
myTree.grow();