function test()
{
test.prototype.color=["red","yellow"];
}
var q1=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
var q2=new test();
alert(q2.color);//red,yellow和function test()
{
test.prototype.color=["red","yellow"];
}
var q1=new test();
var q2=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
alert(q2.color);//red,yellow,green谁能给我解释下为什么两次显示不一样?
{
test.prototype.color=["red","yellow"];
}
var q1=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
var q2=new test();
alert(q2.color);//red,yellow和function test()
{
test.prototype.color=["red","yellow"];
}
var q1=new test();
var q2=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
alert(q2.color);//red,yellow,green谁能给我解释下为什么两次显示不一样?
prototype.color恢复了原属性值第二个是因为共享属性(就是第一行给你解释的)
{
test.prototype.color=["red","yellow"];
}
var q1=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
var q2=new test();
alert(q2.color);//red,yellow
alert(q1.color);
Whenever a function is created, its prototype property is also created according to a specific set of rules.
由于英语不是很强,所以问问。
关于这点你需要费些时间理解理解 这个不是那么容易就彻底弄懂的~~找些相关资料看看~~~有什么不懂的可以具体问prototype不是什么时候构建的。而是这个链上的所有属性在其对象构造之后就可以拥有其上的所有属性和方法了
但这些属性和方法是公有的 而非私有的你在第二次new的时候改变了color 属性
之前的对象的color属性也同时被改变了
当函数被创建(比如你NEW的时候),它的prototype属性也会依据某个特定规则被创建。象1楼所说,new test()后 prototype.color也被重新创建,所以恢复了原设置值
function test()
{
test.prototype.color=["red","yellow"];
}
var q1=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
var q2=new test();
alert(q2.color);//red,yellow这里test.prototype.color=["red","yellow"]; 到底算什么啊???function test()
{
//test.prototype.color=["red","yellow"]; // 这个写里面是什么意思?麻烦解释一下
}
test.prototype.color=["red","yellow"]; // 把这个写外面是作为共享属性了,这个我理解var q1=new test();
q1.color.push("green");
alert(q1.color); //red ,yellow,green
var q2=new test();
alert(q2.color); // red,yellow,green // 这样 q1.color 和 q2.color 一样了