Object.create函数
2015-11-13 16:01
543 查看
Object.create函数 创建一个具有指定原型且可选择性的包含指定属性的对象 Object.create(prototype,descriptors) 参数prototype必需,可以为null descriptors可选,包含一个或多个属性的描述 数据属性:默认都为false writable 是否可以任意写 configurable 是否能够删除、修改 enumerable 是否能用for/in枚举 value 值 访问属性: get() 访问 set() 设置 异常: 1、prototype参数不是对象且不为null 2、descriptors参数中的描述符具有value或writable特性,并具有get或set特性 3、descriptors参数中的描述符具有不为函数的get或set特性 例子一: var obj = Object.create(null,{ size : { value : 'large', enumerable : true //表示当使用for/in语句时,该属性是否会被枚举,默认为false }, shape : { value : 'round', enumerable : true } }) console.log(obj.size); console.log(obj.shape); console.log(Object.getPrototypeOf(obj)); //Object.getPrototypeOf函数可获取原始对象的原型 //large //round //null 例子二: function a(){}; a.prototype.name = 'Jack'; a.prototype.age = '17'; //获取对象a的原型 var obj = Object.create(a.prototype,{ size : { value : 'large', enumerable : true }, shape : { value : 'round', enumerable : true } }) console.log(obj.name); console.log(obj.age); console.log(obj.size); console.log(obj.shape); //Jack //17 //large //round 例子三: var obj = Object.create(null,{ size : { value : 'large', writable : true }, shape : { configurable : false, get : function(){ return 'get' + shape; }, set : function(value){ shape = 'set' + value; } } }) console.log(obj.size); //large obj.size = 'small'; console.log(obj.size); //small console.log(obj.shape); //shape is not defined obj.shape = 'round'; console.log(obj.shape); //getsetround //注意:设置shape值的时候,先通过set,返回setround,取值的时候,又通过get,返回getsetround
参考:https://msdn.microsoft.com/zh-cn/library/ff925952
相关文章推荐
- 自动化测试---PageObjects快速入门(一)
- Qt出现QObject::connect: Cannot queue arguments of type '******'的解决方法
- org.objectweb.asm.ClassVisitor.visit(转载)
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Objective C类方法load和initialize的区别
- ajax获取的全部是object,我要获取的是json
- Objective-C的hook方案(一): Method Swizzling
- 转载 Jackson 高性能的JSON处理 ObjectMapper
- Objective-C基础语法快速入门
- 添加Swift到Objective-c失败
- Objective-C开发编码规范
- Object.keys()枚举属性名
- Objective-C 【随机数】
- [Objective-C]Class类型、class方法
- Kobject、Kset 和 Subsystem
- Objective-C JSON操作
- OC中的小知识点
- Objective-C中将枚举转换成NSString的一种思路
- Objective-c:数据类型
- OC中的成员变量