function A()
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
//B.prototype.z=7; alert(b.z); //0
b=new B();
alert(b.constructor);
B.prototype.z=9;
alert(b.z);//undefined
---------
function A()
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=7; alert(b.z); //0,为什么不是 7 ?原型不是能随时修改,并影响它的所有实例吗?b=new B();
alert(b.constructor); //B.prototype.z=9;
alert(b.z);//7,为什么不是9 ?原型不是能随时修改,并影响它的所有实例吗?
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
//B.prototype.z=7; alert(b.z); //0
b=new B();
alert(b.constructor);
B.prototype.z=9;
alert(b.z);//undefined
---------
function A()
{} function B()
{
B.prototype=new A();
} B.prototype.z=0;
var b=new B();
alert(b.constructor);
B.prototype.z=7; alert(b.z); //0,为什么不是 7 ?原型不是能随时修改,并影响它的所有实例吗?b=new B();
alert(b.constructor); //B.prototype.z=9;
alert(b.z);//7,为什么不是9 ?原型不是能随时修改,并影响它的所有实例吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<script type="text/javascript">
<!--
function A()
{}function B()
{
this.prototype = new A();
}B.prototype.z=0;var b=new B();
alert(b.constructor);B.prototype.z=7;
alert(b.z); //7b=new B();
alert(b.constructor);B.prototype.z=9;
alert(b.z);//9
//-->
</script>
</body>
</html>
function B()
{
this.prototype = new A(); // 增加了名为 prototype 的实例属性,该属性指向一个 A 的实例。
}
lz 应该想做原型继承吧,写法如下:L@_@Kfunction A()
{
this.lastName = "A";
}function B()
{
this.firstName = "B";
}B.prototype = new A();
B.prototype.nickname = "Tom";var b = new B();
document.write(b.firstName, "<br />");
document.write(b.lastName, "<br />");
document.write(b.nickname, "<br />");B.prototype.nickname = "Jerry";b = new B();
document.write(b.firstName, "<br />");
document.write(b.lastName, "<br />");
document.write(b.nickname, "<br />");
只有重新实例化后才能读到新的缺省属性。function A()
{} function B()
{
B.prototype=new A(); // 将原型刷新为一个 A 的实例,破坏了原型链!
}
以下例子可以证明俺滴解释,lz 仔细看下L@_@K
function A()
{
this.name = "A";
}function B()
{
// 错误在于每次实例化后都将原型刷新为一个 A 的实例,破坏了原型链,
// 因此也就无法及时查找属性了,只有重新实例化后才能读到新的缺省属性。
B.prototype=new A();
}alert(B.prototype.name); // undefined,B 仍为原始值,根本没有 name 属性,B.prototype.z = 0;alert(B.prototype.z); // 0,尚未破坏刷新原型。var b = new B();alert(b.z); // 从原型取得 z 属性,然后刷新原型。
alert(B.prototype.z); // undefined,原型已刷新为 new A()!
alert(B.prototype.name); // A 验证 B.prototype 已指向 A 实例!