直接上代码:

klass函数

var klass = function (Parent, props) {
var Child, F, i; //1.新构造函数
Child = function () {
if (Child.uber && Child.uber.hasOwnProperty("__construct")) {
Child.uber.__construct.apply(this, arguments);
}
if (Child.prototype.hasOwnProperty("__construct")) {
Child.prototype.__construct.apply(this, arguments);
}
}; //2.继承
Parent = Parent || Object;
F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.uber = Parent.prototype;
Child.prototype.constructor = Child; //3.添加实现方法
for (i in props) {
if (props.hasOwnProperty(i)) {
Child.prototype[i] = props[i];
}
} //返回该class
return Child;
};

使用实例:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>类式继承</title>
<script src="klass.js"></script>
</head>
<body>
<script>
var Man = klass(null, {
__construct : function (name) {
console.log("Man's constructor!");
this.name = name;
},
getName : function () {
return this.name;
}
}); //var first = new Man('Adam');
//console.log(first.getName()); var SuperMan = klass(Man, {
__construct : function (name) {
console.log("SuperMan's constructor!");
},
getName : function () {
var name = SuperMan.uber.getName.apply(this);
return "I am " + name;
}
}); var clark = new SuperMan('Clark Kent');
console.log(clark.getName()); </script>
</body>
</html>

javascript类式继承函数最优版的更多相关文章

  1. javascript类式继承最优版

    直接看实例代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...

  2. JavaScript 类式继承与原型继承

    交叉着写Java和Javascript都有2年多了,今天来总结下自己所了解的Javascript类与继承. Javascript本身没有类似Java的面向对象的类与继承术语,但其基于原型对象的思想却可 ...

  3. javascript类式继承模式#4——共享原型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. javascript类式继承模式#3——借用和设置原型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. javascript类式继承模式#2——借用构造函数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. javascript类式继承模式#1——默认模式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. javascript 的类式继承(构造函数)

    <script type="text/javascript"> //类式继承(构造函数) var father = function(){ this.age = 52; ...

  8. JavaScript中的类式继承和原型式继承

    最近在看<JavaScript设计模式>这本书,虽然内容比较晦涩,但是细品才发现此书内容的强大.刚看完第四章--继承,来做下笔记. 书中介绍了三种继承方式,类式继承.原型式继承和掺元类继承 ...

  9. 精读JavaScript模式(八),JS类式继承

    一.前言 这篇开始主要介绍代码复用模式(原书中的第六章),任何一位有理想的开发者都不愿意将同样的逻辑代码重写多次,复用也是提升自己开发能力中重要的一环,所以本篇也将从“继承”开始,聊聊开发中的各种代码 ...

随机推荐

  1. 教你如何自学UI设计

    一.常用的UI相关工具软件 PS Adobe Illustrator(AI) C4D AE Axure Sketch 墨刀 Principle Cutterman PxCook Zeplin 蓝湖 X ...

  2. 如何基于 K8S 多租能力构建 Serverless Container

    当前 Kubernetes 已经成为名副其实的企业级容器编排规范,很多云平台都开始提供兼容 Kubernetes 接口的容器服务.而在多用户支持方面,多数平台选择直接提供专属虚机集群,用户需要花费大量 ...

  3. MIT-6.824 MapReduce

    概述 MapReduce是由JeffreyDean提出的一种处理大数据的编程模型,用户定义map和reduce函数,map函数处理原始数据生成一系列键值对中间数据,reduce函数并合相同key的键值 ...

  4. raft--分布式一致性协议

    0. 写在前面的话 一直从事分布式对象存储工作,在分布式对象存储的运营,开发等工作中,数据一致性是至关重要的.因此想写一篇关于分布式一致性的文章.一来,可以和大家分享.二来,可以提高自己的文字提炼能力 ...

  5. 20135234mqy-——信息安全系统设计基础第十二周学习总结

    process environ.c environvar.c consumer.c 管道写端 producer.c 管道读端 testmf.c listargs.c pipedemo.c 管道 pip ...

  6. jqgrid查找

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...

  7. mark1-git

    Administrator@-20131003RY MINGW64 ~ $ pwd /c/Users/Administrator Administrator@-20131003RY MINGW64 ~ ...

  8. Python正则表达式使用

    Python正则表达式使用 参考资料: Python正则表达式| 菜鸟教程 Python正则表达式详解 - 我当道士那儿些年 - 博客园 前言 由于遇到一个提取字符串某个子串的问题,刚开始使用了暴力方 ...

  9. ElasticSearch 2 (13) - 深入搜索系列之结构化搜索

    ElasticSearch 2 (13) - 深入搜索系列之结构化搜索 摘要 结构化查询指的是查询那些具有内在结构的数据,比如日期.时间.数字都是结构化的.它们都有精确的格式,我们可以对这些数据进行逻 ...

  10. Activiti End Event及其派生类使用范例

    http://tynerblain.com/blog/2006/08/11/bpmn-end-events-1/ 普通流程完结就用end event,而比如无效卡支付时,就会进入cancel end  ...