java,
java,
1) 对象冒充
[html] view plain copy print?
//继承第一种方式:对象冒充
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", "1234");
parent.sayHello();
child.sayHello();
child.sayWorld();
2) call方法方式。
call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。
[html] view plain copy print?
<p>//使用call方式实现对象的继承</p><p>function Parent(username)
{
this.username = username;</p><p> this.sayHello = function()
{
alert(this.username);
}
}</p><p>function Child(username, password)
{
Parent.call(this, username);</p><p> this.password = password;</p><p> this.sayWorld = function()
{
alert(this.password);
}
}</p><p>var parent = new Parent("zhangsan");</p><p>var child = new Child("lisi", "123");</p><p>parent.sayHello();</p><p>child.sayHello();
child.sayWorld();</p><p>
</p><span style="font-size:18px;"><span style="color:#000000;"> </span></span>
1) 对象冒充
[html] view plain copy print?
//继承第一种方式:对象冒充
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", "1234");
parent.sayHello();
child.sayHello();
child.sayWorld();
2) call方法方式。
call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。
[html] view plain copy print?
<p>//使用call方式实现对象的继承</p><p>function Parent(username)
{
this.username = username;</p><p> this.sayHello = function()
{
alert(this.username);
}
}</p><p>function Child(username, password)
{
Parent.call(this, username);</p><p> this.password = password;</p><p> this.sayWorld = function()
{
alert(this.password);
}
}</p><p>var parent = new Parent("zhangsan");</p><p>var child = new Child("lisi", "123");</p><p>parent.sayHello();</p><p>child.sayHello();
child.sayWorld();</p><p>
</p><span style="font-size:18px;"><span style="color:#000000;"> </span></span>
好好学习天天向继续努力吧。
JavaScript中的继承。
1) 对象冒充
[html] view plain copy print?
//继承第一种方式:对象冒充
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", "1234");
parent.sayHello();
child.sayHello();
child.sayWorld();
2) call方法方式。
call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。
[html] view plain copy print?
<p>//使用call方式实现对象的继承</p><p>function Parent(username)
{
this.username = username;</p><p> this.sayHello = function()
{
alert(this.username);
}
}</p><p>function Child(username, password)
{
Parent.call(this, username);</p><p> this.password = password;</p><p> this.sayWorld = function()
{
alert(this.password);
}
}</p><p>var parent = new Parent("zhangsan");</p><p>var child = new Child("lisi", "123");</p><p>parent.sayHello();</p><p>child.sayHello();
child.sayWorld();</p><p>
</p><span style="font-size:18px;"><span style="color:#000000;"> </span></span>
相关文章
- 暂无相关文章
用户点评