美文网首页
JSON对象,bind,apply,call

JSON对象,bind,apply,call

作者: 追逐_e6cf | 来源:发表于2018-08-17 19:47 被阅读0次

    1.json对象的创建方式:

      var obj = {};  //字面量/直接量;
      var obj1 = new Object();  //构建;
      var obj2 = Object.create( {} ); 
    

    第一种是用的最频繁的;

    //delete json.name;  //删除对象的一个属性
    

    2.json对象:属性值如果为字符内容,必须要用引号引起来

            var json1 = {
                name :'zs',  // 键值对:   键:属性;  值:属性值
                age :'18',
                tall :'180',
                weight : '75kg',
                style:{
                    color: 'deeppink',
                    backgroundColor: 'pink',
                    width: '500px' 
                },
                1:' first '
            }
    //console.log(json1.name);
    //console.log(json1["name"]);
    //console.log(json1[1]);
    

    3.js对象的序列化和反序列化
    JSON.stringify
    JSON.parse

    //console.log(JSON.stringify(json1));
    //console.log(JSON.parse(json1));
    

    4.检测对象的属性
    in关键字 用关键字in去母串进行匹配,如果有的话,就返回

    if( "name" in json1  ){
      console.log(1);
    }{
      console.log(2);
    }
    

    5.call/apply/bind
    call/apply/bind 改变this的指向
    call/apply 自动执行
    bind 不会自动执行
    如何传参:
    apply 传参数的时候,必须要用数组保存起来
    call/bind 传参数的时候,不需要用数组保存起来,直接传参数
    共同点:第一个值必须为改变this的对象

            function fn( a , b ){
                console.log( this )
                console.log( a + b )
            }
            document.onclick = fn.bind( null ,5, 8 );
            document.onclick = fn.call( json1 , 6,6 )
            document.onclick = fn.apply( json1 , [ 12 ,12 ] )
    

    相关文章

      网友评论

          本文标题:JSON对象,bind,apply,call

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