【JS】显式类型转换

1. 前置知识

  • 原生类型的一些方法处理
  • 若对象中定义了[Symbol.toPrimitive]这个方法,则[Symbol.toPrimitive]这个方法比valueOftoString优先级都要高。进行类型转换时,这个方法会被调用,返回原始值。下面的讨论都是默认没有定义[Symbol.toPrimitive]这个方法的。
  • [Symbol.toPrimitive]方法返回的不是基本类型,则会抛出抛出一个TypeError异常
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return [];
  },
  valueOf() {
    console.log("called valueOf");
    return [];
  },
  [Symbol.toPrimitive](hint) {
    console.log("called toPrimitive");
    return null
  }
};

console.log(0 === Number(obj));
//called toPrimitive
//true
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return [];
  },
  valueOf() {
    console.log("called valueOf");
    return null;
  },
  [Symbol.toPrimitive](hint) {
    console.log("called toPrimitive");
    return []
  }
};

console.log(0 === Number(obj));
//called toPrimitive
//Uncaught TypeError: Cannot convert object to primitive value

2. 显式转换

1.1 String

obj.toString()String(obj)

1.1.1 Number转String

  • 0-0都变成'0'

1.1.2 Object转String

  1. 若对象有toString方法,则调用之
  2. toString方法返回的是 基本数据类型 ,则将值转换为【字符串】并返回
  3. 若对象无toString方法或调用toString方法返回的并不是 基本数据类型,则尝试调用对象的valueOf方法
  4. 若对象有valueOf方法,则调用之
  5. valueOf方法返回的是 基本数据类型,则将值转换为【字符串】并返回
  6. 以上两个方法都不存在或者都不返回基本数据类型,则会抛出抛出一个TypeError异常
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return null;
  },
  valueOf() {
    console.log("called valueOf");
    return null;
  },
};

console.log('null' === String(obj));
//called toString
//true
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return {};//非基本类型
  },
  valueOf() {
    console.log("called valueOf");
    return null;
  },
};

console.log('null' === String(obj));
//called toString
//called valueOf
//true
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return {};//非基本类型
  },
  valueOf() {
    console.log("called valueOf");
    return {};//非基本类型
  },
};

console.log(String(obj));
//called toString
//called valueOf
//Uncaught TypeError: Cannot convert object to primitive value

1.2 Boolean

  • 除了空字符串''±0nulludnefinedNaNfalse,其他都是true,包括new Boolean(false)
Boolean('');//false
Boolean(-0);//false
Boolean(null);//false
Boolean(undefined);//false
Boolean(NaN);//false

Boolean([]); //true
Boolean({});//true
Boolean(new Boolean(false));//true
  • !可以对变量进行Boolean转换并取反
![];//false
!{};//false
!null;//true
!undefined;//true

1.3 Number

1.3.1 String转Number

  • 字符串中含有非数字类型字符返回NaN,其他直接转换为对应数字
console.log(Number("abc"));//NaN
console.log(Number("123"));//123
console.log(Number("123abc"));//NaN

console.log(Number("+123"));//123
console.log(Number("-123"));//-123
console.log(Number('-123.45'));//-123.45
  • 其他特殊情况
console.log(Number("0xff"));//255
console.log(Number("2E4"));//20000
console.log(Number(""));//0

1.3.2 Boolean转Number

  • true1, false0
console.log(Number(new Boolean(false)));//0
console.log(Number(new Boolean(true)));//1
console.log(Number(false));//0
console.log(Number(true));//1

1.3.3 undefined和null

  • undefinedNaN, null0
console.log(Number(undefined));//NaN
console.log(Number(null));//0

1.3.4 Object转Number

  1. 若对象有valueOf方法,则调用之
  2. valueOf方法返回的是 基本数据类型 ,则将值转换为【数字】并返回
  3. 若对象无valueOf方法或调用valueOf方法返回的并不是 基本数据类型,则尝试调用对象的toString方法
  4. 若对象有toString方法,则调用之
  5. toString方法返回的是 基本数据类型,则将值转换为【数字】并返回
  6. 以上两个方法都不存在或者都不返回基本数据类型,则会抛出抛出一个TypeError异常
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return null;
  },
  valueOf() {
    console.log("called valueOf");
    return null;
  },
};

console.log(0 === Number(obj));
//called valueOf
//true
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return null;
  },
  valueOf() {
    console.log("called valueOf");
    return [];
  },
};

console.log(0 === Number(obj));
//called valueOf
//called toString
//true
let obj = {
  name: "Lawson",
  toString() {
    console.log("called toString");
    return [];
  },
  valueOf() {
    console.log("called valueOf");
    return [];
  },
};

console.log(Number(obj));
//called valueOf
//called toString
//Uncaught TypeError: Cannot convert object to primitive value

参考文献:
https://blog.csdn.net/jhzhahuaiyu/article/details/96188502
https://www.cnblogs.com/gwf93/p/10295234.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • js类型转换 1 用于类型转换的valueOf和toString valueOf()的意义是,返回这个对象逻辑上对...
    Super曲江龙Kimi阅读 2,767评论 0 0
  • 本文是lhyt本人原创,希望用通俗易懂的方法来理解一些细节和难点。转载时请注明出处。文章最早出现于本人github...
    lhyt阅读 2,695评论 0 1
  • 前言:js的类型转换真是容易让人一头雾水,接下来我将会好好整理一下。 原始值到原始值(数字,字符串,布尔值)的转换...
    tency小七阅读 2,256评论 0 0
  • 运算符优先级 显示类型转换 Number()Number(undefined(特殊) / null / true ...
    Jolyne_Cujoh阅读 1,306评论 0 0
  • 记录一些常见的坑 js中的6个假值 https://www.jb51.net/article/122992.htm...
    小猪佩奇的王子阅读 1,139评论 0 0

友情链接更多精彩内容