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

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