ECMA5 Object.create
来源:互联网 发布:java正则表达式 可选 编辑:程序博客网 时间:2024/04/30 12:14
Object.create(prototype,descriptors) 是E5中提出的一种新的对象创建方式。
1.参数:
prototype是要继承的原型,如果不是一个子函数,可以传一个null。
descriptors是对象的属性描述符,这个参数是可选的。数据属性是可获取可设置的属性,数据属性描述符包括value,writable,configurable和enumerable特性。如果没有指定,后三个值默认为false。访问器属性包括configurable,enumerable,get和set特性。
2.返回的值
一个以prototype为原型并且包含指定属性的新对象。
3.异常
包含以下三种情况:
(1)prototype不是对象并且不是null
(2)descriptors里面具有value和writable特性,同时还要set和get特性
(3)descriptors里面具有不是函数的set和get特性
4.示例
(1)
var newObj = Object.create(null, { size: { value: "large", enumerable: true }, shape: { value: "round", enumerable: true } });for (var item in newObject){ console.log(item); }//size//shape
(2)
(3)实现继承function Person(name){ this.name=name;} Person.prototype.showName=function(){ console.log(this.name);}var person1=Object.create(Person.prototype,{ name:{ value:"vuturn", enumerable:true, writable:true },job:{value:"software engineer",enumerable:true}});person1.showName();//vuturnperson1.name="John";person1.showName();//Johnperson1.job="doctor";console.log(person1.job);//software engineer
function SuperType(name){ this.name=name;}SuperType.prototype.showName=function(){ console.log(this.name);}function SubType(name){ SuperType.call(this.name);}SubType.prototype=Object.create(SuperType.prototype);var person=new SubType("vuturn");person instanceof SubType;person instanceof SuperType;
0 0
- ECMA5 Object.create
- ECMA5中的Object.defineProperty()方法
- Object.create
- Object.create
- Object.create疑问
- 创建对象Object.Create
- Create C++ Object Dynamically
- Object.create函数
- Object.create polyfill 方法
- Object.create的用法
- [js]object.create图解
- javascript Object.create()函数
- Object.create()详解
- 创建对象(Create Object)
- 关于 JavaScript中的 Object.create()
- Object.create()方法的使用
- 原型式继承-Object.create()
- ECMA5-规则变化
- 日经社説 20150520 南シナ海の安定脅かす中国の埋め立て
- 面试题50 树中两个节点的最低公共祖先LCA(Lowest Common Ancestor )
- hadoop笔记本
- Structual设计--Adapter模式
- 编译动态modules ko的makefile
- ECMA5 Object.create
- MAVEN Erro: java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
- 完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)
- php调用memcache存储session
- #66 Plus One
- 【演讲精华】移动开发者如何白手起家、快速盈利
- (10)用树莓派B+的GPIO接口控制发光二极管闪烁
- resin服务器配置和使用servlet(可用)
- c和指针练习