js中的继承与重写
2016-01-27 09:20
501 查看
rt.
用function 分别定义Person和Account类模型,其中Account从Person继承,并重写toString()方法
end.
用function 分别定义Person和Account类模型,其中Account从Person继承,并重写toString()方法
<script type="text/javascript"> function go() { var acc1 = new Account('Taro', 'Shibuya1-1-2', '1001', 20000); var acc2 = new Account('Hanako', 'Akasaka2-3-4', '1002', 35000); acc1.toString(); acc2.toString(); } // 定义Person构造器 function Person(name, address) { this.name = name; this.address = address; } // 在Person.property中添加toString方法 Person.prototype.toString = function() { document.write(this.name + " " + this.address + "<br>"); } // 定义Account构造器 function Account(name, address, number, amount) { // 从Person继承 this.newObj = Person; this.newObj(name, address); delete this.newObj; // Account特有属性 this.number = number; this.amount = amount; } Account.prototype = Object.create(Person.prototype); // 设置"constructor" 属性指向Account Account.prototype.constructor = Account; // 更改Person中toString方法 Account.prototype.toString = function() { document.write(this.name + " " + this.address + " " + this.amount + "<br>"); } Account.prototype.deposit = function(x) { this.amount += x; } Account.prototype.withdraw = function(x) { this.amount -= x; } </script>
end.
相关文章推荐
- Javascript获取页面元素的位置
- JavaScript+html5 canvas绘制的小人效果
- Objective-C与JavaScript交互的那些事
- jsp页面中文乱码
- youku的js脚本的工具函数和初始化方法
- JavaScript+html5 canvas制作色彩斑斓的正方形效果
- APP中javascript+css3实现下拉刷新效果
- Threejs 的场景查看 - 几个交互事件库助你方便查看场景
- 简单的方式实现javascript 小数取整
- 悟透JavaScript
- Js年龄计算,精确到岁、月、小时
- js正则表达式lastIndex
- js正则表达式语法
- 重读js高程笔记二
- JavaScript+html5 canvas绘制的小人效果
- js实现简单排列组合的方法
- 基于JavaScript实现瀑布流效果(循环渐近)
- JavaScript下的时间格式处理函数Date.prototype.format
- js实现的彩色方块飞舞奇幻效果
- javascript中的3种继承实现方法