JavaScript之继承
来源:互联网 发布:网络语音肝是啥意思 编辑:程序博客网 时间:2024/06/09 15:08
JavaScript之继承
js的继承_基于原型链方式的继承
var Person = { name: "李华", say: function () { alert(this.name); }}function clone(obj) { var F = function () {}; F.prototype = obj; return new F();}var Student = clone(Person);Student.no = "";Student.books = [];Student.say = function () { alert(this.no + " " + this.name);}Student.showBooks = function () { alert(this.books);}var stu1 = clone(Student);stu1.no = "001";stu1.name = "凯乐"stu1.books.push("java");stu1.say();stu1.showBooks();var stu2 = clone(Student);stu2.no = "002";stu2.name = "王明"stu2.books.push("JavaScript");stu2.say();stu2.showBooks(); //结果是:java JavaScript
js的继承_基于类声明的方式的继承
比较好的继承方式,不过对于初学者可能会比较难理解
涉及到call、prototype等知识。
var People = function (name, age) { this.name = name; this.age = age; this.books = [];}People.prototype.say = function () { alert(this.name + " " + this.age + " " + this.books);}People.prototype.getBooks = function () { return this.books;}var Student = function (name, age, no) { this.no = no; Student.parentClass.constructor.call(this, name, age); } //使用extend方法完成继承extend(Student, People);/**********用extend方法*************/function extend(childClass, parentClass) { var F = function () {}; F.prototype = parentClass.prototype; childClass.prototype = new F(); childClass.parentClass = parentClass.prototype;}var stu1 = new Student();stu1.name = "小二";stu1.age = 22;stu1.books.push("java");stu1.say();var stu2 = new Student();stu2.name = "大宝";stu2.age = 34;stu2.books.push("HTML");stu2.say();
1 0
- JavaScript基础之继承
- JavaScript之继承方式
- 精通Javascript 之 继承
- JavaScript之继承方式
- javascript之继承
- JavaScript之继承
- JavaScript之五:继承
- JavaScript之----继承
- JavaScript之继承
- javascript学习之继承
- JavaScript之继承
- JavaScript之继承
- JavaScript之单继承与多继承
- 深入剖析Javascript之继承
- javascript框架之继承机制
- javascript继承方式之二
- javascript继承方式之三
- javascript学习之对象继承
- address list
- HDU 5969 最大的位或
- 装tensorflow未果
- 已知两个链表A和B分别表示两个集合,其元素递增排列。请设计算法求出两个集合A和B的差集(即仅由在A中出现而不在B中出现的元素所构成的集合),并以同样的形式存储,同时返回该集合的元素个数。
- 树莓派和pc通过网线建立内网
- JavaScript之继承
- boost::function用法
- 多态&强制类型转换&抽象类和接口
- 初学如何构建一个小型项目的流程与心得
- 分布式环境下限流方案的实现
- 贪心算法
- 完美解决谷歌浏览器 adobe flash player 已过期
- LDD时间,延时及延缓操作-时间及获取当前时间
- maven spring springmvc 项目