17. JavaScript中的继承。
1) 对象冒充
<html>
<head>
<script type="text/javascript">
function Parent(username) {
this.username = username;
this.sayHello = function() {
alert(this.username);
}
}
function Child(username, password) {
//下面三行代码时重点
this.method = Parent;
this.method(username);
delete this.method;
this.password = password;
this.sayWorld = function() {
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi", "123");
parent.sayHello();
child.sayHello();
child.sayWorld();
</script>
</head>
<body>
</body>
</html>
parent中的this已经被child替换,所以才可以实现这个功能。
2) call方法方式。
call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。
<html>
<head>
<script type="text/javascript">
function Parent(username) {
this.username = username;
this.sayHello = function() {
alert(this.username);
}
}
function Child(username, password) {
Parent.call(this, username);
this.password = password;
this.sayWorld = function() {
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi", "123");
parent.sayHello();
child.sayHello();
child.sayWorld();
</script>
</head>
<body>
</body>
</html>
3) apply方法方式
下面这个就相当于call的简化写法,它把参数都放在了数组里面。
<html>
<head>
<script type="text/javascript">
function Parent(username) {
this.username = username;
this.sayHello = function() {
alert(this.username);
}
}
function Child(username, password) {
Parent.apply(this, new Array(username));
this.password = password;
this.sayWorld = function() {
alert(this.password);
}
}
var parent = new Parent("zhangsan");
var child = new Child("lisi", "123");
parent.sayHello();
child.sayHello();
child.sayWorld();
</script>
</head>
<body>
</body>
</html>
4)原型链方式(无法给构造函数传参数)
<html>
<head>
<script type="text/javascript">
function Parent() {
}
Parent.prototype.hello = "hello";
Parent.prototype.sayHello = function() {
alert(this.hello);
}
function Child(username, password) {
}
Child.prototype = new Parent();
Child.prototype.world = "world";
Child.prototype.sayWorld = function() {
alert(this.world);
}
var child = new Child();
child.sayHello();
child.sayWorld();
</script>
</head>
<body>
</body>
</html>
5)混合方式(推荐)
下面方法的好处是,即不共享变量,同时使用的还是同一个函数。
<html>
<head>
<script type="text/javascript">
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();
</script>
</head>
<body>
</body>
</html>
分享到:
相关推荐
JavaScript的核心特性包括其弱类型、原型继承以及事件驱动的非阻塞I/O模型,使得它在构建交互式网页时非常高效。随着Node.js的出现,JavaScript也进入了服务器端编程,实现了全栈开发的可能性。 在"findYourDev...
9. Java应用开发:JavaScript是一种脚本语言,通常不使用Java编写,而Applet、Servlet和Java Swing都是Java技术栈的一部分。 10. 访问控制:如果没有指定访问控制符,成员变量默认为包级私有,即同一包内的所有类都...
- **WebView类**:这是Android中的核心类,继承自View,提供了加载网页、执行JavaScript等功能。 - **Chromium内核**:从Android 4.4(KitKat)开始,WebView基于Google的Chromium项目,提供更快的渲染和更多的...
- 继承性:一个类可以从另一个类继承属性和行为,实现代码复用。 - 多态性:不同对象对同一消息可以有不同的响应,允许使用父类引用指向子类对象。 2. **类的声明**: - 类名应遵循驼峰命名法,首字母大写,如`...
3. PowerBuilder的主要特点是事件驱动、可视化、面向对象,支持系统对象和用户对象的属性、事件、方法以及封装性、继承性、多态性。 4. 支持多种数据库,包括Oracle、Sybase、DB2、Informix、Access等,同时自带...
14. **类的特性**:类具有继承、派生和多态性,对象的生命周期包括构造和析构阶段。 15. **类的成员**:包括事件、方法和属性。 16. **命名空间**:用于解决C#中变量和函数的命名冲突。 17. **数据结构**:C#提供...