Object.create
2016-03-15 00:16
477 查看
Object.create(parent):
三件事: 1. 创建一个对象
2. 继承指定父对象
3. 为新对象扩展新属性
何时使用create: 希望在创建对象时就提前指定继承的父对象,并同时扩展新属性时。
作业: 完成课堂练习:
1. 创建hmm对象继承father,同时扩展phone属性
2. Object.create的实现原理:
三件事: 1. 创建一个对象
2. 继承指定父对象
3. 为新对象扩展新属性
何时使用create: 希望在创建对象时就提前指定继承的父对象,并同时扩展新属性时。
作业: 完成课堂练习:
1. 创建hmm对象继承father,同时扩展phone属性
2. Object.create的实现原理:
<span style="font-size:18px;"> //不可能实现所有浏览器都支持Objecet.create,一部分可重写 //Objecet.create 的实现步骤3 //自定义Object.create方法 if(Object.mycreate===undefined){ Object.mycreate=function(parent,props){ <span style="background-color: rgb(255, 102, 102);">//1:创建一个新对象 var obj=new Object(); //2:设置新对象继承parent Object.setPrototypeOf(obj,parent);//IE9+ //3:为新对象扩展属性</span> Object.defineProperties(obj,props);//IE9+ ES5的新标准 //返回新对象 return obj; } } var father={bal:1000,car:"=b="} //var hmm=Object.create(father); var hmm=Object.mycreate(father,{//create调用的也是defineproperty phone:{//默认为false value:"肾6s", writable:true, enumertable:true, configurable:true. }, }); console.log(hmm.hasOwnProperty("bal"));//false,可以用,但是存到父类那里 console.log(hmm.bal);//子继承父,也可扩展自己的属性 console.log(hmm.car); console.log(hmm.phone);</span>
相关文章推荐
- Object: 多个属性的集合
- c++中的对象引用(object reference)与对象指针的区别
- 关于android 的org.json类JsonObject解析值为null的出现的问题
- Object-c 类、对象方法
- 在object,embed上添加跳转链接(视频上添加跳转)
- ObjectInputStream解析
- iOS开发系列—Objective-C之Foundation框架
- IE浏览器利用ActiveXObject对象对文件系统进行操作
- JDK升级到8.0之后,启动报错Unable to process Jar entry [jdk/nashorn/internal/objects/NativeRegExp$Prototype.cla
- FLEX中SharedObject介绍及应用
- Objective-C之优雅的命名
- objective-c感悟(二)内存管理
- js 判断Object 是否为空问题解决方案
- 6、在MyJDBCUtils中加入update(String sql , Object[] args)方法
- 【iOS】Effective Objective-C
- iOS开发之Objective-c的MD5/SHA1加密算法的实现
- Objective-C Runtime 运行时之三:方法与消息
- 怎么解析json串在.net中[Error reading JObject from JsonReader. Current JsonReader item is not an obj]
- TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement
- Objective-C Runtime