java web学习笔记(javascript继承)
2011-11-14 19:42
302 查看
1、对象冒充,不推荐使用
function Parent(username) {
this.nameuser = username;
this.sayHello() {
alert(username);
}
}
function Child(username, password) {
this.method = Parent;
this.method(username);
delete this.method;
}
2、使用call方法,Function对象中的方法
function say(str) {
alert(this.name + '=' + str);
}
var ob = new Object();
obj.name = 'JPass';
say.call(obj, 'long'); // 此方法其实是将obj对象赋值给了this
3、使用apply方法实现对象的继承
function Parent(username) {
this.nameuser = username;
this.sayHello() {
alert(username);
}
}
function Child(username, password) {
Parent.apply(this, new Array(username));
}
4、使用原型链的方式是想对象间的继承
function Parent() {
}
Parent.prototype.name = 'JPass';
Parent.prototype.sayName = function() {
alert(this.name);
}
function Child() {
}
Child.prototype = new Parent();
function Parent(username) {
this.nameuser = username;
this.sayHello() {
alert(username);
}
}
function Child(username, password) {
this.method = Parent;
this.method(username);
delete this.method;
}
2、使用call方法,Function对象中的方法
function say(str) {
alert(this.name + '=' + str);
}
var ob = new Object();
obj.name = 'JPass';
say.call(obj, 'long'); // 此方法其实是将obj对象赋值给了this
3、使用apply方法实现对象的继承
function Parent(username) {
this.nameuser = username;
this.sayHello() {
alert(username);
}
}
function Child(username, password) {
Parent.apply(this, new Array(username));
}
4、使用原型链的方式是想对象间的继承
function Parent() {
}
Parent.prototype.name = 'JPass';
Parent.prototype.sayName = function() {
alert(this.name);
}
function Child() {
}
Child.prototype = new Parent();
相关文章推荐
- javascript 实现继承
- javascript 继承
- TDD测试驱动的javascript开发(3) ------ javascript的继承
- javascript继承详解(一):前世今生
- javascript 继承、子类化
- 利用javascript中的call实现继承
- JavaScript入门之原型继承篇
- javascript继承模式原理与示例深入剖析
- [置顶] 老生常谈javascript的5中继承
- JavaScript 原型与继承机制详解
- Javascript面向对象编程(二):构造函数的继承
- javascript的原型和继承(1)
- 转载:Javascript面向对象编程(三):非构造函数的继承
- javascript继承
- javascript继承
- JavaScript的面向对象机理2)-继承
- JavaScript的模块化:封装(闭包),继承(原型) 介绍
- JavaScript 原型与继承说明
- JavaScript中的原型继承
- JavaScript继承详解(一)