js object.create()、Object.defineproperty()、,Object.keys()...for..in,for..each

Speak of that how to create object in javascript , maybe you know:

Object.keys() : return array of object attribute that can't be enumerable
new Object();
var obj = {};
new function(){}; // var obj = new function(){}();
Have you know Object.create()?

这里写图片描述

//as the photo you needn’t to care all is object and proto all assign Object , you can read somethind else

这里写图片描述

Accord to above , you know when you new a obj , that is ,the obj proto will have a constructor assign object .

Note: you can use construtor.prototype ={} , to set obj proto assign , but this can’t be used in where object.

Object.create() ; default param

    {
        //http://www.jb51.net/article/77039.htm
        //http://blog.csdn.net/sinat_22996989/article/details/49559513
        // the consructor need return undifined but obj otherwise will get the new obj thar you reurn
        function cons(){
            let args = Array.prototype.slice.call(arguments);
            this.name = args[0];
            this.love = args[1];
            this.say =()=>console.log(this.name);
            this.play=()=>console.log(this.love)
        }

        let o = new cons(`qi`,`play game`);



        // you can create obj base on null just like object.create(null,{propertiesObject});
        var obj = Object.create(o,{
            nickName:{
                value:'kongdaluoli',
            },
        });

        //check can enmu
        for(let key in obj){
            console.log(`check out the obj key is ${key}`);
        }

        window.oo = obj;

        var d= {};
        d.prototype.say=function () {
            console.log("sb")
        }


        //new a obj then use prototype

    }```
   //enumerable: false
   //configurable: false
   //writable: false

这里写图片描述

so, when you use for…in will get : (not found nickName attribute)

Object.defineProperty

Note :use Object.defineProperty if you have used writable or value before will not set the function of set and get at the same time, you can use it for obj exit attribute or add new attribute . it’s convenient that use Object.defineProperties to define multiple attribute .

     function cons(){
                let args = Array.prototype.slice.call(arguments);
                this.name = args[0];
                this.love = args[1];
                this.say =()=>console.log(this.name);
                this.play=()=>console.log(this.love)
            }

        let o = new cons(`qi`,`play game`);


        // you can create obj base on null just like object.create(null,{propertiesObject});
        var obj = Object.create(o,{
            nickName:{
                value:'kongdaluoli',
            },
        });

        Object.defineProperty(obj,"wanna",{
            value:"want back",
            configurable:false,
        })


            //if you have set someone attribute configurable:false , will not set the descriptor again
    //        Object.defineProperty(obj,"wanna",{
    //            value:"want back",
    //            configurable:false,
    //            enumerable:true,
    //        })


     Object.defineProperty(obj,"wanna1",{
                value:"want back",
                configurable:false,
                enumerable:true,
            })

        Object.defineProperties(obj,{
            pros:{
                value : 123,
                enumerable:true,
            },
            pros1:{
                value : 1234,
            },
        })
        //check can enmu
        for(let key in obj){
            console.log(`check out the obj key is ${key}`);
        }

        window.oo = obj;

At Last , introduce , freeze,seal and extension

Refer : https://segmentfault.com/a/1190000003894119

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值