扩展基础类型的函数
interface String {
toBool: () => boolean;
}
String.prototype.toBool = function (): boolean {
return this.valueOf() === '1' || this.valueOf().toUpperCase() === 'TRUE' || false;
}
其中 valueof() 取的才是真实的string,而不是 this === ‘1’ ,this代表 {"1"} 对象;
另外,this == "1" 也可以实现toBool 的功能,内部实现了 {"1"} 转换为 "1",但是不推荐使用;
-----------------------------------------------------------------------------------------------------------------
其他:
判断基础类型:用 typeof(t) === "string" | "number" ...
var t = "1";
typeof(t) === "string" // true
typeof(t) === "String" // false