js 深拷贝、浅拷贝与数据的类型判断
2018-03-06 17:14
537 查看
一、浅拷贝
二、深拷贝
三、数据类型判断
1、Array.isArray() 用于确定传递的值是否是一个 Array
Array.isArray([1, 2, 3]); // true Array.isArray({foo: 123}); // false Array.isArray("foobar"); // false Array.isArray(undefined); // false
如果对象是 Array,则为true; 否则为false。
Polyfill
假如不存在 Array.isArray(),则在其他代码之前运行下面的代码将创建该方法。if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }; }
2、typeof
数据类型 | Type |
---|---|
Undefined | “undefined” |
Null | object” |
布尔值 | “boolean” |
数值 | “number” |
字符串 | “string” |
Symbol (ECMAScript 6 新增) | “symbol” |
宿主对象(JS环境提供的,比如浏览器) | Implementation-dependent |
函数对象 | “function” |
任何其他对象 | “object” |
// Numbers typeof 37 === 'number'; typeof 3.14 === 'number'; typeof Math.LN2 === 'number'; typeof Infinity === 'number'; typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写,意思是"不是一个数字" typeof Number(1) === 'number'; // 不要这样使用! // Strings typeof "" === 'string'; typeof "bla" === 'string'; typeof (typeof 1) === 'string'; // typeof返回的肯定是一个字符串 typeof String("abc") === 'string'; // 不要这样使用! // Booleans typeof true === 'boolean'; typeof false === 'boolean'; typeof Boolean(true) === 'boolean'; // 不要这样使用! // Symbols typeof Symbol() === 'symbol'; typeof Symbol('foo') === 'symbol'; typeof Symbol.iterator === 'symbol'; // Undefined typeof undefined === 'undefined'; typeof blabla === 'undefined'; // 一个未定义的变量,或者一个定义了却未赋初值的变量 // Objects typeof {a:1} === 'object'; // 使用Array.isArray或者Object.prototype.toString.call方法可以从基本的对象中区分出数组类型 typeof [1, 2, 4] === 'object'; typeof new Date() === 'object'; // 下面的容易令人迷惑,不要这样使用! typeof new Boolean(true) === 'object'; typeof new Number(1) ==== 'object'; typeof new String("abc") === 'object'; // 函数 typeof function(){} === 'function'; typeof Math.sin === 'function';
就是typeof来判断数据类型其实并不准确。比如数组、正则、日期、对象的typeof返回值都是object,这就会造成一些误差。
所以在typeof判断类型的基础上,我们还需要利用 Object.prototype.toString 方法来进一步判断数据类型
toString方法和typeof方法返回值的区别
数据 | toString | typeof |
---|---|---|
Tables | Are | Cool |
“foo” | String | string |
new String(“foo”) | String | object |
new Number(1.2) | Number | object |
true | Boolean | boolean |
new Boolean(true) | Boolean | object |
new Date() | Date | object |
new Error() | Error | object |
new Array(1, 2, 3) | Array | object |
/abc/g | RegExp | object |
new RegExp(“meow”) | RegExp | object |
instanceof
用来判断某个构造函数的prototype属性是否存在于另外一个要检测对象的原型链上。// 定义构造函数 function C(){} function D(){} var o = new C(); // true,因为 Object.getPrototypeOf(o) === C.prototype o instanceof C; // false,因为 D.prototype不在o的原型链上 o instanceof D; o instanceof Object; // true,因为Object.prototype.isPrototypeOf(o)返回true C.prototype instanceof b9b7 Object // true,同上 C.prototype = {}; var o2 = new C(); o2 instanceof C; // true o instanceof C; // false,C.prototype指向了一个空对象,这个空对象不在o的原型链上. D.prototype = new C(); // 继承 var o3 = new D(); o3 instanceof D; // true o3 instanceof C; // true
constructor 属性
<script type="text/javascript"> var test=new Array(); if (test.constructor==Array) { document.write("This is an Array"); } if (test.constructor==Boolean) { document.write("This is a Boolean"); } if (test.constructor==Date) { document.write("This is a Date"); } if (test.constructor==String) { document.write("This is a String"); } </script>
输出:
This is an Array
<script type="text/javascript"> function employee(name,job,born) { this.name=name; this.job=job; this.born=born; } var bill=new employee("Bill Gates","Engineer",1985); document.write(bill.constructor); </script>
输出
function employee(name, job, born)
{this.name = name; this.job = job; this.born = born;}
相关文章推荐
- js数据类型和数组判断
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- js判断数据类型
- js中关于数据类型的判断
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- js判断各种数据类型
- js-判断数据类型
- js中数据类型判断
- js判断数据类型
- js 判断各种数据类型
- js 判断各种数据类型
- 判断JS数据类型的四种方法
- js数据类型判断 toString方法的底层分析
- 【js基础】javascript中关于数据类型一些知识和类型判断
- js基本数据类型及判断方法
- JS判断数据是否是JSON类型
- 如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较
- JS 判断数据类型(兼容)
- js判断数据类型 小计
- 工作日常--js引用类型数据深拷贝与浅拷贝