JavaScript 学习三 继承
2010-06-06 18:06
267 查看
//继承第一种方式:对象冒充 /* function Parent(username) { this.username = username; this.hello = function() { alert(this.username); } } function Child(username, password) { this.method = Parent; this.method(username);//最关键的一行 delete this.method; //Parent(username); this.password = password; this.world = function() { alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi", "123456"); parent.hello(); child.hello(); child.world(); */ //继承第二种方式:call()方法方式 /* function test(str) { alert(this.name + " " + str); } var object = new Object(); object.name = "zhangsan"; test.call(object,"langsin"); function Parent(username) { this.username = username; this.hello = function() { alert(this.username); } } function Child(username, password) { Parent.call(this,username); this.password = password; this.world = function() { alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi", "123456"); parent.hello(); child.hello(); child.world(); */ //继承第二种方式:apply()方法方式 function Parent(username) { this.username = username; this.hello = function() { alert(this.username); } } function Child(username, password) { Parent.apply(this,new Array(username)); this.password = password; this.world = function() { alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi", "123456"); parent.hello(); child.hello(); child.world(); /* //原型链 function Parent() { } Parent.prototype.hello = "hello"; Parent.prototype.sayHello = function() { alert(this.hello); } function Child() { } Child.prototype = new Parent(); Child.prototype.world = "world"; Child.prototype.sayWorld = function() { alert(this.world); } var child = new Child(); child.sayHello(); child.sayWorld(); //混合方式 function Parent(hello) { this.hello = hello; } Parent.prototype.sayHello = function() { alert(this.hello); } function Child(hello, world) { Parent.call(this, hello); this.world = world; } Child.prototype = new Parent(); Child.prototype.sayWorld = function() { alert(this.world); } var child = new Child('hello', 'world'); child.sayHello(); child.sayWorld(); */ function Shape(edge) { this.edge = edge; } Shape.prototype.getArea = function() { return -1; } Shape.prototype.getEdge = function() { return this.edge; } function Triangle(bottom, height) { Shape.call(this, 3); this.bottom = bottom; this.height = height; } Triangle.prototype = new Shape(); Triangle.prototype.getArea = function() { return 0.5 * this.bottom * this.height; } /* Triangle.prototype.getEdge = function() { return this.edge; }*/ var triangle = new Triangle(10,4); alert(triangle.getEdge()); // 3 alert(triangle.getArea()); // 20 function Rectangle(bottom, height) { Shape.call(this, 4); this.bottom = bottom; this.height = height; } Rectangle.prototype = new Shape(); Rectangle.prototype.getArea = function() { return this.bottom * this.height; } /* Rectangle.prototype.getEdge = function() { return this.edge; } */ var rectangle = new Rectangle(10,20); alert(rectangle.getEdge()); //4 alert(rectangle.getArea()); //200
相关文章推荐
- 学习NodeJS第五天:JavaScript的继承
- JavaScript学习13 JavaScript中的继承
- JavaScript学习笔记二十二:class继承
- 轻松学习JavaScript十三:JavaScript基于面向对象之继承(包含面向对象继承机制)
- JavaScript ES6新的类继承特性学习笔记
- javascript基础学习三:原型继承
- JavaScript高级程序设计学习笔记--面向对象的程序设计(二)-- 继承
- Javascript 设计模式学习笔记(2) - 继承(Inheritance) (下)
- JavaScript学习总结(二十)——Javascript非构造函数的继承
- 【JavaScript学习】面向对象的程序设计(3):原型链继承
- JavaScript 学习_4_原型_对象_继承
- web前端学习,JavaScript中对象继承方法你了解多少呢?
- javascript学习——继承
- Javascript学习笔记九——prototype封装继承
- 关于JavaScript的面向对象和继承有利新手学习
- 韩顺平 javascript教学视频_学习笔记19_js面向对象三大特征(封装,继承,多态)
- 阅读Ext 学习Javascript(二)Core/Ext.extend 从继承说起
- javascript面向对象学习笔记(一)——继承
- Javascript学习笔记9——prototype封装继承
- 从零开始学习前端JAVASCRIPT — 14、闭包与继承