相思资源网 Design By www.200059.com
本文实例讲述了JavaScript实现多态和继承的封装操作。分享给大家供大家参考,具体如下:
封装Encapsulation
如下代码,这就算是封装了
(function (windows, undefined) { var i = 0;//相对外部环境来说,这里的i就算是封装了 })(window, undefined);
继承Inheritance
(function (windows, undefined) { //父类 function Person() { } Person.prototype.name = "name in Person"; //子类 function Student() { } Student.prototype = new Person(); //修复原型 Student.prototype.constructor = Student; //构造函数 Student.prototype.supr = Person.prototype; //父类 //创建子类实例 var stu = new Student(); Student.prototype.age = 28; Student.prototype.name = "name in Student instance"; //打印子类成员及父类成员 console.log(stu.name); //name in Student instance console.log(stu.supr.name); //name in Person console.log(stu.age); //28 })(window, undefined);
使用在线HTML/CSS/JavaScript代码运行工具 http://tools.jb51.net/code/HtmlJsRun,运行结果如下:
多态Polymorphism
有了继承,多态就好办了
//这就是继承了 (function (windows, undefined) { //父类 function Person() { } Person.prototype.name = "name in Person"; Person.prototype.learning = function () { console.log("learning in Person") } //子类 function Student() { } Student.prototype = new Person(); //修复原型 Student.prototype.constructor = Student; //构造函数 Student.prototype.supr = Person.prototype; //父类 Student.prototype.learning = function () { console.log("learning in Student"); } //工人 function Worker() { } Worker.prototype = new Person(); //修复原型 Worker.prototype.constructor = Worker; //构造函数 Worker.prototype.supr = Person.prototype; //父类 Worker.prototype.learning = function () { console.log("learning in Worker"); } //工厂 var personFactory = function (type) { switch (type) { case "Worker": return new Worker(); break; case "Student": return new Student(); break; } return new Person(); } //客户端 var person = personFactory("Student"); person.learning(); //learning in Student person = personFactory("Worker"); person.learning(); //learning in Worker })(window, undefined);
使用在线HTML/CSS/JavaScript代码运行工具 http://tools.jb51.net/code/HtmlJsRun,运行结果如下:
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无JavaScript实现多态和继承的封装操作示例的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。