近期前端复习笔记0523

 

01 typeof 

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

语法:jquery

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测试

语法:this

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

返回:spa

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

注意.net

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

 

03 toString.call()

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

语法:code

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/50325323https://www.itcodemonkey.com/article/3440.html
相关文章
相关标签/搜索