惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - 牦牛

Linux常用命令 错误:80040154 没有注册类 的问题 [转]Java实现定时任务的三种方法 访问IIS网站需要输入用户名密码(非匿名登录)问题汇总 IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题 端口占用问题 中国电信翼支付网关接口接入 修改Tomcat响应请求时返回的Server内容 模拟用户点击弹出新页面 Eclipse常用设置 Tomcat服务无法启动的问题 Windows Server 2008 小操作汇总 手机也能连VPN,再来个远程控制PC这种事你以为我会随便说么! - 牦牛 Windows下Git服务器搭建及使用过程中的一些问题 Oracle PL/SQL中的循环处理(sql for循环) CSS的display小记 博客园添加访问人数统计 IIS故障问题(Connections_Refused)分析及处理 Windows Server 2008自定义桌面
JavaScript继承的模拟实现
牦牛 · 2013-07-26 · via 博客园 - 牦牛

  我们都知道,在JavaScript中只能模拟实现OO中的"类",也就意味着,在JavaScript中没有类的继承。我们也只能通过在原对象里添加或改写属性来模拟实现。

先定义一个父类,

1         //父类
2         function ParentClass() {
3             this.className = "ParentClass";
4             this.auth = "Auth";
5             this.version = "V1.0";
6             this.parentClassInfo = function () {
7                 return this.className + "\n" + this.auth + "\n" + this.version;
8             }
9         }

一、prototype 实现:

 1         //子类
 2         //1、prototype继承
 3         function ChildClassByPrototype() {
 4             this.date = "2013-07-26";
 5             this.classInfo = function () {
 6                 return this.parentClassInfo() + "\n" + this.date;
 7             }
 8         }
 9 
10         ChildClassByPrototype.prototype = new ParentClass();
11 
12         var cctest1 = new ChildClassByPrototype();
13         cctest1.parentClassInfo();
14         cctest1.classInfo();

   这种方式很简单,只需把父类的实例赋值给子类的prototype属性就行了,然后子类就可以使用父亲的方法和属性了。这里其实是用到了原型链向上查找的特性,比如这个例子中的 cctest1.parentClassInfo() 方法,JavaScript会先在ChildClassByPrototype的实例中查找是否有parentClassInfo()方法,子类中没有,所以继续查找ChildClassByPrototype.prototype属性,而其prototype属性的值是ParentClass的一个实例,该实例有parentClassInfo()方法,于是查找结束,调用成功。

二、apply 实现:

1         //2、apply继承
2         function ChildClassByApply() {
3             ParentClass.apply(this, new Array());//ParentClass.apply(this, []);
4             this.date = "2013-07-26";
5             this.classInfo = function () {
6                 return this.parentClassInfo() + "\n" + this.date;
7             }
8         }

   JavaScript中的apply可以理解为用A方法替换B方法,第一个参数为B方法的对象本身,第二个参数为一个数组,该数组内的值为需要传递给A方法对应的参数列表,如果参数为空,即没有参数传递,可通过 new Array()、[] 来传递

三、call + prototype 实现:

1         //3、call+prototype继承
2         function ChildClassByCall() {
3             ParentClass.call(this, arguments);
4             this.date = "2013-07-26";
5             this.classInfo = function () {
6                 return this.parentClassInfo() + "\n" + this.date;
7             }
8         }
9         ChildClassByCall.prototype = new ParentClass();

  callapply作用类似,即都是用A方法替换B方法,只是传递的参数不一样,call方法的第一个参数为B方法的对象本身,后续的参数则不用Array包装,需直接依次进行传递。既然作用差不多,为何多了一句 原型赋值呢?这是因为call方法只实现了方法的替换而没有对对象属性进行复制操作。

  每种方法都有其适用环境,比如,如果父类带有有参构造函数:

1         function ParentClass(className, auth, version) {
2             this.className = className;
3             this.auth = auth;
4             this.version = version;
5             this.parentClassInfo = function () {
6                 return this.className + "\n" + this.auth + "\n" + this.version;
7             }
8         }

这种情况下,prototype就不适用了,可选用apply或call;

 1         function ChildClassByApply(className, auth, version) {
 2             ParentClass.apply(this, [className, auth, version]);
 3             this.date = "2013-07-26";
 4             this.classInfo = function () {
 5                 return this.parentClassInfo() + "\n" + this.date;
 6             }
 7         }
 8 
 9
10         function ChildClassByCall(className, auth, version) {
11             ParentClass.call(this, arguments[0], arguments[1], arguments[2]); //ParentClass.call(this, className, auth, version);
12             this.date = "2013-07-26";
13             this.classInfo = function () {
14                 return this.parentClassInfo() + "\n" + this.date;
15             }
16         }
17         ChildClassByCall.prototype = new ParentClass();

实例化:

1        var cctest2 = new ChildClassByApply("ParentClass", "Auth", "V1.0");
2        var cctest3 = new ChildClassByCall("ParentClass", "Auth", "V1.0");

  在apply和call中,又该如何取舍呢?在OO的继承中,子类继承于父类,那么它应该也是父类的类型。即,ChildClassByCall、ChildClassByApply应该也是ParentClass类型,但我们用"instanceof"检测一下就会发现,通过apply继承的子类,并非ParentClass类型。所以,我们建议用call + prototype 来模拟实现继承。据说,Google Map API 的继承就是使用这种方式哟。


参考资料:http://www.w3school.com.cn/js/pro_js_inheritance_implementing.asp