js object.create()、Object.defineproperty()、,Object.keys()...for..in,for..each
来源:互联网 发布:淘宝网 支付宝的功能 编辑:程序博客网 时间:2024/04/27 18:00
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
- js object.create()、Object.defineproperty()、,Object.keys()...for..in,for..each
- Object.defineProperty()/Object.keys()
- (for..in)、Object.keys()和Object.getOwnPropertyNames(),for...of
- JS中的Object.assign()、Object.create()、Object.defineProperty()
- hasOwnProperty,Object.keys(),propertyIsEnumerable,for-in(loop)
- for in 、Object.keys()以及Object.getOwnPropertyNames的区别
- JS的Object.defineProperty
- Object.defineProperty
- Object.defineProperty
- Object.defineProperty
- Object.defineProperty
- Object.defineProperty
- Object.defineProperty()
- Javascript研究: 遍历对象的方式Object.getOwnPropertyNames vs Object.keys vs for ...in
- 详解js之Object.defineProperty
- js中的Object.defineProperty方法
- js Object.keys
- JS Object.keys()
- Python中*args 和**kwargs的用法
- JAVA多线程之——读写锁 ReentrantReadWriteLock
- 算法提高 金属采集
- Java之BigDecimal类使用
- 遇到的一个关于base64编码的小问题
- js object.create()、Object.defineproperty()、,Object.keys()...for..in,for..each
- 系统学习javaweb-01-java基础语法
- C# 多个TcpClient 的区分处理
- 简单说说zlib.h,libz.a/libz.so库和-lz
- linux下C语言my_strcat实现
- BigDecimal讲解
- 第六届蓝桥杯——垒骰子(矩阵快速幂)
- 在AndroidStudio中导入SlidingMenu并使用
- D. Least Cost Bracket Sequence (贪心)