JavaScript一个类继承中实现
JavaScript类是默认原型对象继承:
var Person = function() {
this.name = "people";
this.hello = function() {
console.log("hello user:" + this.name);
}
} var User = function() {
this.name = "user";
this.hello = function() {
User.prototype.hello.call(this, arguments);
console.log("hello user:" + this.name);
}
} User.prototype = new Person();
new User().hello();
有没有一种方法。可以让JavaScript的类像Java那样,通过一个superkeyword即调用父类的方法,于是我这样扩展了Function:
(function() {
Function.prototype.extend = function(baseClass) { // this is a function object.
var oldPrototype = this.prototype, newPrototype = {}, _super = new baseClass(); //inherits all properties and methods.
for ( var name in _super) {
newPrototype[name] = _super[name];
} for ( var name in oldPrototype) {
// only override properties in this new Class.
if (oldPrototype.hasOwnProperty(name)) {
// only function need _super.
if (typeof oldPrototype[name] == "function" && typeof _super[name] == "function") {
newPrototype[name] = (function(name, fn) {
return function() {
var tmp = this._super; // set super method
this._super = _super[name]; var ret = fn.apply(this, arguments); this._super = tmp; return ret;
};
})(name, oldPrototype[name]);
}
}
} this.prototype = newPrototype; return this;
};
})();
測试代码
var A = function() {
this.hello = function() {
console.log("hello, I'm A");
}
}; var B = function() {}; B.prototype = {
hello : function() {
this._super(); console.log("hello, I'm B");
}
}; B.extend(A); var C = function() {}; C.prototype = {
hello : function() {
this._super(); console.log("hello, I'm C");
}
}; C.extend(B); var b = new B();
var c = new C(); //b.hello();
c.hello();
结果
hello, I'm A
hello, I'm B
hello, I'm C
版权声明:本文博客原创文章。博客,未经同意,不得转载。
JavaScript一个类继承中实现的更多相关文章
- C++ //多继承语法 C++中允许一个类继承多个类
1 //多继承语法 C++中允许一个类继承多个类 2 #include <iostream> 3 #include <string> 4 using namespace std ...
- C++类继承中的构造函数和析构函数 调用顺序
思想: 在C++的类继承中,构造函数不能被继承(C11中可以被继承,但仅仅是写起来方便,不是真正的继承) 建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推: 析构对象时, ...
- (C++)C++类继承中的构造函数和析构函数
思想: 在C++的类继承中, 建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推: 析构对象时,其顺序正好与构造相反: 例子: #include <iostream& ...
- C++ 类的继承四(类继承中的重名成员)
//类继承中的重名成员 #include<iostream> using namespace std; /* 自己猜想: 对于子类中的与父类重名的成员,c++编译器会单独为子类的这个成员变 ...
- java一个类 继承HttpServlet 和实现Servlet区别
java一个类 继承HttpServlet 和实现Servlet区别 servlet 是一个接口,如果实现这个接口,那么就必须实现接口里面定义的所有方法 而HttpServlet实现了servlet接 ...
- C# 类继承中的私有字段都去了哪里?
最近在看 C++ 类继承中的字段内存布局,我就很好奇 C# 中的继承链那些 private 字段都哪里去了? 在内存中是如何布局的,毕竟在子类中是无法访问的. 一:举例说明 为了方便讲述,先上一个例子 ...
- C++ 类的继承五(类继承中的static关键字)
//类继承中的static关键字 #include<iostream> using namespace std; /* 派生类中的静态成员 基类定义的静态成员,将被所有派生类共享 根据静态 ...
- C# 类型运算符重载在类继承中的调用测试
这是一篇晦涩难懂的片面的研究 一,简单的继承层次 class CA { } class CB : CA{ } class CC : CB{ } } void Test(CA oa){//CATest ...
- C++基础——类继承中方法重载
一.前言 在上一篇C++基础博文中讨论了C++最基本的代码重用特性——类继承,派生类可以在继承基类元素的同时,添加新的成员和方法.但是没有考虑一种情况:派生类继承下来的方法的实现细节并不一定适合派生类 ...
随机推荐
- Partitioner分区过程分析
Partition中国人意味着分区,意义的碎片,这个阶段也是整个MapReduce该过程的第三阶段.在Map返回任务,是使key分到通过一定的分区算法.分到固定的区域中.给不同的Reduce做处理,达 ...
- USACO maze1 BFS
不写了很长的时间bfs该,很长一段时间的中间失误,当延期一次延伸成功的新节点的节点应该被标记为参观.否则,在某些情况下无限期延长队列. 输入一个小坑爹处理称号,能够进来当字符串被读取.然后用周围的墙上 ...
- oracle 11g 基于磁盘的备份rman duplicate
基于磁盘的备份rman duplicate 命令创建standby database 前提条件: 确保原始库数据库的备份.存档standby 结束是完全可见, 这里,如果原始文库和靶 - 侧数据文件, ...
- Delphi 3D Glscene安装
GLScene开源库Delphi基于提供OpenGL的3D框架.由GLScene组件,您可以轻松地创建和渲染你的3D幕后.令人奇怪的是,.对于这样一个很好的开源库.该网络无法找到完整的安装说明,甚至G ...
- Group By去除重复数据
今天在写一个sql,目的是去除表里某一个字段相同的数据,只保留最新的一条.之前group by 用的少.特此记录一下. SELECT * FROM litb_approval_task SELECT ...
- 让UIAlertController兼容的同时iphone和ipad
让UIAlertController兼容的同时iphone和ipad by 吴雪莹 var alert = UIAlertController(title: nil, message: message ...
- ubunut 查看port被哪个程序占用
查看8087port被哪个程序占用 lsof -i :8087 -n
- Kd-Tree算法原理和开源实现代码
本文介绍一种用于高维空间中的高速近期邻和近似近期邻查找技术--Kd-Tree(Kd树). Kd-Tree,即K-dimensional tree,是一种高维索引树形数据结构,经常使用于在大规模的高维数 ...
- Nagios显示器MySQL一个错误:NRPE: Unable to read output具体的解决过程
前言:nagios介面.见监测mysql服务错误,如下面: Warning:NRPE: Unable to read output 1,跟nagios显示器server上check下 1.1.运行ch ...
- CSS定位与层叠
position:static(静态定位) 当position属性定义为static时,可以将元素定义为静态位置,所谓静态位置就是各个元素在HTML文档流中应有的位置 podisition定位 ...