JavaScript中的 toString

官方: https://developer.mozilla.org/zh-CN/search?q=toString

Object 中有 toString() 方法 以下对象 重写了自己的 toString()方法
  1. location.toString() 方法返回包含整个URL的USVString。它是Location.href的只读版本。
// Let's imagine an <a id="myAnchor" href="https://developer.mozilla.org/en-US/docs/Location/toString"> element is in the document
var anchor = document.getElementById("myAnchor");
var result = anchor.toString(); // Returns: 'https://developer.mozilla.org/en-US/docs/Location/toString'
  1. String.prototype.toString() 返回指定对象的字符串形式。和 String.prototype.valueOf() 方法返回值一样。字符串对象
var x = new String("Hello world");

alert(x.toString())      // 输出 "Hello world"
  1. URL.toString()
const url = new URL("https://developer.mozilla.org/en-US/docs/Web/API/URL/toString");
url.toString() // 应当返回字符串形式的 URL
  1. Date.prototype.toString() 返回一个字符串,表示该Date对象。
    总是返回一个美式英语日期格式的字符串。当一个日期对象被用来作为文本值或用来进行字符串连接时,toString 方法会被自动调用。toString() 是通用函数。如果不是Date实例,则 返回"Invalid Date"。
const event = new Date('August 19, 1975 23:15:30');

console.log(event.toString());
// expected output: Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)
// (note: your timezone may vary)
  1. Function.prototype.toString() 返回一个表示当前函数源代码的字符串。
function sum(a, b) {
  return a + b;
}

console.log(sum.toString());
// expected output: "function sum(a, b) {
//                     return a + b;
//                   }"

console.log(Math.abs.toString());
// expected output: "function abs() { [native code] }"
  1. Boolean.prototype.toString() 返回指定的布尔对象的字符串形式。
const flag1 = new Boolean(true);

console.log(flag1.toString());
// expected output: "true"

const flag2 = new Boolean(1);

console.log(flag2.toString());
// expected output: "true"
  1. Symbol.prototype.toString() 返回当前 symbol 对象的字符串表示。
  • symbol 原始值不能转换为字符串 只能先转换成它的包装对象,再调用 toString() 方法:
Symbol("foo") + "bar";
// TypeError: Can't convert symbol to string
Symbol("foo").toString() + "bar"
// "Symbol(foo)bar",就相当于下面的:
Object(Symbol("foo")).toString() + "bar"
// "Symbol(foo)bar"
  1. Number.prototype.toString() 返回指定 Number 对象的字符串表示形式。
numObj.toString([radix]) 
//指定要用于数字到字符串的转换的基数(从2到36)。如果未指定 radix 参数,则默认值为 10。
  • Number 对象覆盖了 Object 对象上的 toString() 方法,它不是继承的 Object.prototype.toString()。对于 Number 对象,toString() 方法以指定的基数返回该对象的字符串表示。

  • 如果转换的基数大于10,则会使用字母来表示大于9的数字,比如基数为16的情况,则使用a到f的字母来表示10到15。

  • 如果基数没有指定,则使用 10。

  • 如果对象是负数,则会保留负号。即使radix是2时也是如此:返回的字符串包含一个负号(-)前缀和正数的二进制表示,不是 数值的二进制补码。

  • 进行数字到字符串的转换时,建议用小括号将要转换的目标括起来,防止出错。

var count = 10;

console.log(count.toString());    // 输出 '10'
console.log((17).toString());     // 输出 '17'
console.log((17.2).toString());   // 输出 '17.2'

var x = 6;

console.log(x.toString(2));       // 输出 '110'
console.log((254).toString(16));  // 输出 'fe'

console.log((-10).toString(2));   // 输出 '-1010'
console.log((-0xff).toString(2)); // 输出 '-11111111'
  1. Array.prototype.toString() 返回一个字符串,表示指定的数组及其元素。当一个数组被作为文本值或者进行字符串连接操作时,将会自动调用其 toString 方法。
const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());
// expected output: "1,2,a,1a"

  1. BigInt.prototype.toString()
  2. URLSearchParams.toString()
  3. Error.prototype.toString() 返回一个指定的错误对象(Error object)的字符串表示。
var e = new Error("fatal error");
print(e.toString()); // "Error: fatal error"

e.name = undefined;
print(e.toString()); // "Error: fatal error"

e.name = "";
print(e.toString()); // "fatal error"

e.message = undefined;
print(e.toString()); // "Error"

e.name = "hello";
print(e.toString()); // "hello"
Object.prototype.toString()
  • Object.prototype.toString() 返回一个表示该对象的字符串。每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString() 方法被每个 Object 对象继承。如果此方法在自定义对象中未被覆盖,toString() 返回 “[object type]”,其中 type 是对象的类型。以下代码说明了这一点:
var o = new Object();
o.toString(); // returns [object Object]
  • 注意:如的ECMAScript 5 和随后的 Errata 中所定义,从 JavaScript 1.8.5 开始,toString() 调用 null 返回[object Null],undefined 返回 [object Undefined]。请参阅下面的使用 toString() 检测对象类型。
  • 可以自定义一个方法,来取代默认的 toString() 方法。该 toString() 方法不能传入参数,并且必须返回一个字符串。自定义的 toString() 方法可以是任何我们需要的值,但如果它附带有关对象的信息,它将变得非常有用。
  • 使用 toString() 检测对象类型
var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值