js 继承并重写方法示例
2016-09-09 15:14
288 查看
用function 分别定义Person和Student类模型,其中Student从Person继承,并重写toString()方法
// 定义Person构造器 function Person(name) { this.name = name; } // 在Person.property中添加toString方法 Person.prototype.toString = function() { document.write(this.name); } // 定义Student构造器 function Student(name, course) { // 从Person继承 this.newObj = Person; this.newObj(name); delete this.newObj; // Student特有属性 this.course = course; } Student.prototype = Object.create(Person.prototype); // 设置"constructor" 属性指向Student Student.prototype.constructor = Student; // 更改Person中toString方法 Student.prototype.toString = function() { document.write(this.name + " " + this.course); }
相关文章推荐
- js继承中的方法重写
- js---17继承中方法属性的重写
- C#继承类重写与隐藏基类方法的区别
- Java中继承接口重写同名方法?
- 继承之虚方法和重写方法知识点和思考练习
- C#下 子类继承父类的虚方法后 使用override重写父类方法 和 不使用override重写有什么区别?
- js继承的4种方法
- 重写——上转型对象不能操作子类新增成员变量和方法但可以操作子类继承和重写的方法
- JavaScript中继承的一些示例方法与属性参考
- js方法重写
- Js Call方法详解(js 的继承)
- asp.net继承page类重写方法 实现最基本的用户登录验证 权限验证等
- C#继承类重写与隐藏基类方法的区别
- 继承HashMap类,重写了toString()方法。
- 关于C#中,继承类重写与隐藏基类方法的区别!
- js 重写trim方法
- [导入]C#继承类重写与隐藏基类方法的区别
- 简单的JS多重继承示例
- YUI中js的继承示例
- 【转】JS方法重写