一、今日所学内容
谈谈对深浅拷贝的理解 ?
变量的复制 基本类型:传递的是值的拷贝 引用类型:传递地址的拷贝
参数的传递 基本类型:传递的是值的拷贝 引用类型:传递地址的拷贝
ES5继承:寄生组合式继承
答: 深浅拷贝都解决了引用类型 数据共享的问题 (如果是引用类型,
两个对象就会共享一份数据,只要修改其中一个对象,就会对另外一个对象造成影响)
但是解决程度不一样 : 浅拷贝 只拷贝一层 深拷贝 --每一层都拷贝
数据类型检测的几种方式以及各自优缺点?
优点:基本的都是能辨识 缺点:有几个辨识一样 如下:
01- typeof:主要用于检测基本类型
console.log(typeof null); //object
console.log(typeof {}); //object
!!console.log(typeof []); --- //object
! ! console.log(typeof function () { }); //function
02-instanceof:
语法:
变量(实例对象) instanceof 构造函数
变量的原型链上找得到后面的构造函数 就会返回true 否则返回false
优点:[] 判断为true了 缺点 一堆true 没得辨识
如下:
!! console.log([] instanceof Array); //true
console.log(function () { } instanceof Function); //true
console.log(function () { } instanceof Object); //true
console.log(10 instanceof Object); //false
console.log(new Number(10) instanceof Object); //true
03- Object.prototype.toString()
//.call()用来改变this指向
console.log(Object.prototype.toString); // ƒ toString() { [native code] }
console.log(Object.prototype.toString()); // [object Object]
var arr = [];
console.log(Object.prototype.toString.call(arr)); [object Array]
console.log(Object.prototype.toString.call(null));
精准辨识 但是需要改变this指向
|