例如 直接用toString():
console.log(true.toString());//"true" console.log(false.toString());//"false" console.log((1).toString());//"1" var str = "test123y"; console.log(str.toString());//"test123y" var arr = ["tom",12,"rose",18]; console.log(arr.toString());//"tom,12,rose,18" var patten = new RegExp("\\[hbc\\]at", "gi"); console.log(patten.toString());//"/\[hbc\]at/gi"
用Object.prototype.toString.call():
Object.prototype.toString.call("abc");//"[object String]" Object.prototype.toString.call(123);//"[object Number]" Object.prototype.toString.call(true);//"[object Boolean]" var arr = [1,2,3]; Object.prototype.toString.call(arr);//"[object Array]"
为什么Object.prototype.toString.call()会返回类型 而不是像toString()一样返回对象内容的字符串型式?
Whistle2
相关分类