美文网首页
Array.prototype.slice.call和数组判断的

Array.prototype.slice.call和数组判断的

作者: 落花夕拾 | 来源:发表于2019-07-21 21:40 被阅读0次

    1、将具有length属性的对象转换成数组

    注意对象一定要
    1.有length属性
    2.属性Key为0,1,2...等number

    //错误使用


    Array.prototype.slice.call.png

    //正确使用


    Array.prototype.slice.call_1.png

    2、3 个判断数组的方法:

    Object.prototype.toString.call() 、
    instanceof 、
    Array.isArray()

    Object.prototype.toString.call()

    1、首先来理解下toString和String的区别
    相同点:都是可以转换为字符串类型

    //不同点”
    1、
    (1).toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined;
    
    let a = 1
    a.toString()
    "1"
    let b = true
    b.toString()
    "true"
    let c = undefined
    c.toString()
    VM268:1 Uncaught TypeError: Cannot read property 'toString' of undefined
        at <anonymous>:1:3
    (anonymous) @ VM268:1
    let d = null
    d.toString()
    VM356:1 Uncaught TypeError: Cannot read property 'toString' of null
        at <anonymous>:1:3
    (anonymous) @ VM356:1
    let e = [1,2]
    e.toString()
    "1,2"
    let f ={'abc' :1,'bcd':2}
    f.toString()
    "[object Object]"
    
    2、String()可以将null和undefined转换为字符串,但是没法转进制字符串
    
    String(a)
    "1"
    String(b)
    "true"
    String(c)
    "undefined"
    String(d)
    "null"
    String(e)
    "1,2"
    String(f)
    "[object Object]"
    
    //3、以重写的方式实现了toString()方法:
    
    //(3.1)重写Date对象的toString()
    Date.prototype.toString=function(){
        console.log('执行了toString(),这里返回数字1');
        return 1;
    };
    //重写Date对象的valueOf()方法
    Date.prototype.valueOf=function(){
        console.log('执行了valueOf()方法,这里返回数字2');
        return 2;
    };
    var date=new Date();
    var str=String(date);
    
    date
    VM546:3 执行了toString(),这里返回数字1
    1
    str
    "1"
    //(3.2)重写Date对象的toString()
    Date.prototype.toString=function(){
        console.log('执行了toString(),这里返回数字1');
        return {};
    };
    //重写Date对象的valueOf()方法
    Date.prototype.valueOf=function(){
        console.log('执行了valueOf()方法,这里返回数字2');
        return 2;
    };
    var date=new Date();
    var str=String(date);//先调用toString()方法,由于返回的不是基本数据类型,再次调用valueOf()方法
    
    VM582:3 执行了toString(),这里返回数字1
    VM582:8 执行了valueOf()方法,这里返回数字2
    
    
    

    2、对所有基本的数据类型进行判断,

    Object.prototype.toString.call('a') 
    "[object String]"
    Object.prototype.toString.call(1) 
    "[object Number]"
    Object.prototype.toString.call(Symbol(1)) 
    "[object Symbol]"
    Object.prototype.toString.call(null)
    "[object Null]"
    Object.prototype.toString.call(undefined)
    "[object Undefined]"
    Object.prototype.toString.call(function(){}) 
    "[object Function]"
    Object.prototype.toString.call({name: 'a'}) 
    "[object Object]"
    Object.prototype.toString.call([]) 
    "[object Array]"
    
    
    instanceof

    instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上
    //缺陷

    function obj(){}
    obj instanceof Object
    //true
    []  instanceof Array; 
    //true
    []  instanceof Object
    //true
    
    
    Array.isArray()

    功能:用来判断对象是否为数组

    let array = []
    undefined
    Array.isArray(array)
    true
    

    相关文章

      网友评论

          本文标题:Array.prototype.slice.call和数组判断的

          本文链接:https://www.haomeiwen.com/subject/ftqvlctx.html