数据类型判断 转换 深浅拷贝

173 阅读3分钟

数据类型简介及深浅拷贝

数据类型 MDN 介绍得已经很清楚 不再赘述 developer.mozilla.org/zh-CN/docs/…
赋值深浅拷贝可移步juejin.cn/post/691723…

如何判断

- typeof

用typeof来检查基本数据类型时,返回的是对应的类型字符串值

1 console.log(typeof ""); //string
2 console.log(typeof 1); //number
3 console.log(typeof true); //boolean
4 console.log(typeof null); //object
5 console.log(typeof undefined);//undefined
6 console.log(typeof []);//object
7 console.log(typeof function(){});//function
8 console.log(typeof {});//object

typeof null === 'object' // true null是唯一一个用typeof检测会返回object的基本类型值

简单理解:

不同的对象在底层都表示为二进制
在JavaScript中二进制前三位为0的话都会被判断为object类型null 的二进制表示全是0,自然前三位也是0,所以 typeof null === “object” typeof除了能判断基本类型和object之外,还能判断function类型,函数也属于对象

typeof Function; // 'function'
typeof new Function(); // 'function'
typeof function() {}; // 'function'

typeof Array; // 'function'
typeof Array(); // 'object'
typeof new Array(); // 'object'
typeof []; // 'object'

typeof Boolean; // "function"
typeof Boolean(); // "boolean"
typeof new Boolean(); // "object"

typeof Math; // 'object'
typeof Math(); // Math is not a function
typeof new Math(); // Math is not a constructor

使用场景

基本数据类型和function类型

- object(实例对象) instanceof constructor(构造函数)

instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。(注意是实例对象)


const simpleStr = "This is a simple string";
const myString  = new String();
const newStr    = new String("String created with constructor");
const myDate    = new Date();
const myObj     = {};
const myNonObj  = Object.create(null);

simpleStr instanceof String; // 返回 false, 非对象实例,因此返回 false
myString  instanceof String; // 返回 true
newStr    instanceof String; // 返回 true
myString  instanceof Object; // 返回 true

myObj instanceof Object;    // 返回 true, 尽管原型没有定义
({})  instanceof Object;    // 返回 true, 同上
myNonObj instanceof Object; // 返回 false, 一种创建非 Object 实例的对象的方法

myString instanceof Date; //返回 false

myDate instanceof Date;     // 返回 true
myDate instanceof Object;   // 返回 true
myDate instanceof String;   // 返回 false

//自定义对象
function f(name) {
    this.name=name;
}
var f1=new f('martin');
console.log(f1 instanceof f);//true

自我检测一波

console.log("1" instanceof String)
console.log(1 instanceof Number) 
console.log(true instanceof Boolean) 
console.log([] instanceof Array) 
console.log(function () { } instanceof Function) 
console.log({} instanceof Object)
console.log(null instanceof Null);
console.log(undefined instanceof Undefined);

使用场景

除基本数据类型 undefined null 以外的引用数据类型
instanceof可以弥补Object.prototype.toString.call()不能判断自定义实例化对象的缺点。

constructor

prototype对象有一个constructor属性,默认指向prototype对象所在的构造函数。由于constructor属性定义在prototype对象上面,意味着可以被所有实例对象继承。

 console.log(("1").constructor === String);
 console.log((1).constructor === Number);
 console.log((true).constructor === Boolean);
 console.log(([]).constructor === Array);
 console.log((function() {}).constructor === Function);
 console.log(({}).constructor === Object);

使用场景

不改变原型的情况下可判断除 undefined null以为的数据类型

Object.prototype.toString.call()

const testObj = Object.prototype.toString;
        console.log(testObj.call("test"));
        console.log(testObj.call(2));
        console.log(testObj.call(true));
        console.log(testObj.call(null));
        console.log(testObj.call(undefined));
        console.log(testObj.call([]));
        console.log(testObj.call({}));
        console.log(testObj.call(function () {}));

输出结果

image.png

使用场景

常用于判断浏览器内置对象
不能精准判断自定义对象,对于自定义对象只会返回[object Object]

function f(name) {
    this.name=name;
}
var f1=new f('martin');
console.log(Object.prototype.toString.call(f1));//[object Object]

数据类型转换

运行机制

首先检测对象的Symbol.toPrimitive 获取其原始值
如果没有这个属性 则继续调用它的valueOf,获取原始值
如果值不是原始值 则继续调用toString转化为字符串
再把字符串基于Number转化为数字

转换为false的情况

  • false
  • 0
  • undefined
  • Null
  • NaN
  • “”

强制转换

  • .toString() || String()
  • Number() parseInt() parseFloat()
  • Boolean()

隐式转换

  • 加号运算符 一方是字符串类型,另一方就会被转为字符串类型
const a = "4"
const b = "2"
const c = "字符串"
const d = 1
console.log(a + b); // 此处应注意 结果为 "42"
console.log(a + c); // "4字符串"
console.log(a + d); // 41

  • 减乘除%
  • 对非布尔值类型的数据求布尔值 !if && ||
  • == 与 ===

常见的误区是“== 检查值是否相等,=== 检查值和类型是否相等”
正确的解释是:“== 允许在相等比较中进行强制类型转换,而 === 不允许。 建议平常多用===以避免不必要的类型转换

凭谁问:廉颇老矣,尚能饭否?