使用Object.create()实现继承
一、常见继承方式
我们日常开发中常见的继承方式主要有: 1、默认模式:
Child.prototype = new Parent();
2、借用构造函数:
function Child(a, b, c, d) {
Parent.apply(this, arguments);
}
3、借用和设置原型:
function Child(a, b, c, d) {
Parent.apply(this, arguments);
}
Child.prototype = new Parent();
4、共享原型:
Child.prototype = Parent.prototype;
5、使用临时构造函数:
var Proxy = function() {};
Proxy.prototype = Parent.prototype;
Child.prototype = new Proxy();
6、extend属性复制:
function extend(parent, child) {
child = child || {};
for(var key in parent) {
if(parent.hasOwnProperty(key)) {
child[key] = parent[key];
}
}
return child;
}
当然在一些javascript库中(jQuery),还存在浅复制和深复制。 7、原型继承模式:
Object.create(Parent);
二、Object.create实现继承
本文将来学习第七种继承方式Object.create()方法来实现继承,关于此方法的详细描述,请戳这里。下面来通过几个实例来学习该方法的使用:
var Parent = {
getName: function() {
return this.name;
}
}
var child = Object.create(Parent, {
name: { value: "Benjamin"},
url : { value: "http://www.zuojj.com"}
});
//Outputs: Object {name: "Benjamin", url: "http://www.zuojj.com", getName: function}
console.log(child);
//Outputs: Benjamin
console.log(child.getName());
我们再来看一个例子,再添加一个继承:
var Parent = {
getName: function() {
return this.name;
},
getSex: function() {
return this.sex;
}
}
var Child = Object.create(Parent, {
name: { value: "Benjamin"},
url : { value: "http://www.zuojj.com"}
});
var SubChild = Object.create(Child, {
name: {value: "zuojj"},
sex : {value: "male"}
})
//Outputs: http://wwww.zuojj.com
console.log(SubChild.url);
//Outputs: zuojj
console.log(SubChild.getName());
//Outputs: undefined
console.log(Child.sex);
//Outputs: Benjamin
console.log(Child.getName());
通过上面可以看出Object.create()方法实现了链式继承,及原型链的继承。如果在控制台打印出各个生成的对象,可以很清楚的看到。
//Outputs: true
console.log(Child.isPrototypeOf(SubChild));
//Outputs: true
console.log(Parent.isPrototypeOf(Child));
isPrototypeOf() 方法测试一个对象是否存在于另一个对象的原型链上。 以上就是本文对Object.create方法的描述,文中不妥之处,还望批评指正。
使用Object.create()实现继承的更多相关文章
- 使用Object.create()实现继承 用 Object.create实现类式继承
使用Object.create()实现继承:https://www.cnblogs.com/cuew1987/p/4075027.html 用 Object.create实现类式继承:https:// ...
- 使用 Object.create实现js 继承
二.Object.create实现继承 本文将来学习第七种继承方式Object.create()方法来实现继承,关于此方法的详细描述,请戳这里.下面来通过几个实例来学习该方法的使用: var Pare ...
- javascript Object.create()究竟发生了什么
这是我在博客园的第一篇博客,早上看了一个大牛的博客,关于javascript继承的,对于大牛使用Object.create()实现继承的方式觉得点问题,就自己研究了一下,所以就有了这篇帖子. 本帖 ...
- js Object.create 初探
1.作用 Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. https://developer.mozilla.org/zh-CN/docs/W ...
- 使用Object.create 克隆对象以及实现单继承
var Plane = function () { this.blood = 100; this.attack = 1; this.defense = 1; }; var plane = new Pl ...
- 构造函数创建对象和Object.create()实现继承
第一个方法用构造函数创建对象,实现方法的继承 /*创建一个对象把所有公用的属性和方法,放进去*/ function Person() { this.name = "W3cplus" ...
- js继承之Object.create()
通过 Object.create() 方法,使用一个指定的原型对象和一个额外的属性对象创建一个新对象.这是一个用于对象创建.继承和重用的强大的新接口.说直白点,就是一个新的对象可以继承一个对象的属性, ...
- js 继承,Object.setPrototypeOf | Object.getPrototypeOf | Object.create class
https://juejin.im/post/5cfd9d30f265da1b94213d28#heading-14 https://juejin.im/post/5d124a12f265da1b91 ...
- 使用 Object.create 创建对象,super 关键字,class 关键字
ECMAScript 5 中引入了一个新方法:Object.create().可以调用这个方法来创建一个新对象.新对象的原型就是调用 create 方法时传入的第一个参数: var a = {a: 1 ...
随机推荐
- 关于RouterOS 国内DDNS服务
虽然RouterOS 加入了cloud功能,但最近在配置RB2011的时候发现不好使,更新域名后无法正确解析到我的IP地址,虽然在cloud的public address中显示了正确的公网ip地址,但 ...
- javascript中 关于eval的那些事
javascript中的eval是一个非常灵活,但是灵活是伴随着风险的. 一.下面我们来看看那使用eval声明变量的问题. function test(x){ eval("var a=x;& ...
- javascript使用bind指定接收者
var json = { jArray: [], jPush: function (c) { this.jArray.push(c); } } var examp = ["123" ...
- IO在block级别的过程分析
btt User Guide在百度找了3天没找到,bing也不行,结果google第一页第5个结果就是. 可恶的GFW http://www.fis.unipr.it/doc/blktrace-1.0 ...
- linux中grep工具
正则表达式 以前我们用grep在一个文件中找出包含某些字符串的行,比如在头文件中找出一个宏定义.其实grep还可以找出符合某个模式(Pattern)的一类字符串.例如找出所有符合xxxxx@xxxx. ...
- Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 是由 ...
- centos7上安装rar解压软件
(http://www.rarlab.com)官网可以查看最新的版本 wget https://www.rarlab.com/rar/rarlinux-x64-5.5.0.tar.gz 2.tar.g ...
- OpenMP 循环调度 + 计时
▶ 使用子句 schedule() 来调度循环,对于循环中每次迭代时间不相等的情况较为有效 ● 代码 #include <stdio.h> #include <stdlib.h> ...
- Direcshow相关资料
CCapture directshow 视频捕获类: http://blog.csdn.net/xgx198831/article/details/7284618 ICaptureGraphBuild ...
- 【python】 time模块和datetime模块详解 【转】
一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...