TypeScript 笔记 打印 Map1234567let myMap = new Map<string, number>([ ["a", 1], ["b", 2], ["c", 3]]);console.log(JSON.stringify(Object.fromEntries(myMap))); // 输出:{"a":1,"b":2,"c":3} 保留两位小数123456789let num = 3.14159;// 使用 toFixed 进行四舍五入并保留两位小数,返回的是一个字符串let numStr = num.toFixed(2);// 将结果转换为数字let roundedNum = parseFloat(numStr);console.log(roundedNum); // 输出: 3.14 Map 构造时初始化1234567const myMap = new Map<string, number>([ ["one", 1], ["two", 2], ["three", 3]]);console.log(myMap.get("one")); // 输出: 1 返回一个类的类型123456789101112131415class Person { name: string; constructor(name: string) { this.name = name; }}function getClassType(): typeof Person { return Person; // 返回类}const PersonClass = getClassType();const personInstance = new PersonClass("Alice");console.log(personInstance.name); // 输出: Alice 解析 typeof Person 获取 类的类型,而 不是实例类型。 getClassType() 返回的是 Person 这个类,而不是 new Person() 的实例。 TypeScript TypeScript 网页重定向 301、302、307、308 的区别 上一篇 TypeScript forEach 中断问题 下一篇