您的位置:首页 > Web前端 > JavaScript

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: