aileLiu

—— on the way home....

近期前端复习笔记0523

 

01 typeof 

参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof

语法:

typeof XXXX(XXXX表达式)
 or
typeof (XXXX)

返回:

返回一个字符串(表示未经计算的操作数的类型),常用于判断数据类型(只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。)

注意:

null 返回 "object",
[] 返回 "object"// 数组被认为是一个对象

 

02 instanceof 

参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/instanceof

语法:

XXXX(要检测的对象)instanceof XXXX(XX构造函数)

返回:

返回 bool,常用于测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性

注意

var myString = new String();
myString instanceof String; // 返回 true
myString instanceof Object; // 返回 true

 

03 toString.call()

参考:https://www.cnblogs.com/wyaocn/p/5796142.html

语法:

toString.call(XXXX) 
or
Object.prototype.toString.call(XXXX)

返回:

返回一个字符串("[object Null]"表示null类型),常用于判断数据类型

注意:

自定义类型 返回 "[object Object]",
eg.自定义类型如下
function Person(name, age) {
    this.name = name;
    this.age = age;
}
var person = new Person("Rose", 18);
Object.prototype.toString.call(arr); //”[object Object]” // 很明显这种方法不能准确判断person是Person类的实例,而只能用instanceof 操作符来进行判断(如下所示)
console.log(person instanceof Person);//输出结果为true

 

04 constructor

参考:http://www.w3school.com.cn/jsref/jsref_constructor_array.asp

语法:

XXXX.constructor

返回:

返回对创建此对象的数组函数的引用,常用于判断数据类型

注意:

[] 返回 function Array() { [native code] }(用 if(test.constructor==Array) 判断)
true 返回 function Boolean() { [native code] }(用 if(test.constructor==Boolean) 判断)

 

05 其他判断数组方法

  1. Array.isArray() 用于确定传递的值是否是一个 Array。参考 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

  2. $.isArray()函数用于判断指定参数是否是一个数组。参考 http://www.runoob.com/jquery/misc-isarray.html

 




*参考链接:
https://blog.csdn.net/u011374890/article/details/50325323
https://www.itcodemonkey.com/article/3440.html


posted on 2018-05-24 22:27  aileLiu  阅读(105)  评论(0编辑  收藏  举报

导航