62,615
社区成员
发帖
与我相关
我的任务
分享
[quote=引用 28 楼 shine333 的回复:] 其实,对有些能帮助理解java语言表象的“错误解释”不必太当真。 如果,某个理解,连java语法基础都颠覆了的话........
说实话,除了那个继承的我看出是扯淡的了,其它的我看着还真不知道,不是我说他们说的对,而是我的水平没到家,唉!!!基础很重要啊!!!楼主好帖!!!不过能麻烦给解释一下不???嘿嘿嘿
咋不靠谱了尼?
[quote=引用 40 楼 lcf 的回复:] [quote=引用 38 楼 weilimicrver 的回复:] 我一直无法理解js中 function test(){ this.a=function(){ return ""; } } test.prototype.a=function(){ return ""; }; var test={ a:function(){ reurn ""; }; }; 他们的本质区别
function base () { }
test.prototype.a = function () {return "";}
function extended () {}
extended.prototype = new base();
var extObj1 = new extended();
var extObj2 = new extended();
alert (extObj1.a()); // ""
extObj1.a = function() { return "haha";}
alert (extObj1.a()); // "haha"
// here is the magic part
base.prototype.a = function() {return "hey, here is the magic!";}
alert(extObj2.a()); // "hey, here is the magic!"
alert(extObj1.a()); // "haha"
简单来说,新建对象的属性是prototype的属性的引用,改变了prototype对象,新建的对象也被改变,并且这种改变是可传递的。如果是function test() {
this.a = function() {return "";}
}
你就没有这种改变prototype的属性就影响到所有实例对象的便利了
好啦好啦我知道这里是JavaSE板[quote=引用 38 楼 weilimicrver 的回复:] 我一直无法理解js中 function test(){ this.a=function(){ return ""; } } test.prototype.a=function(){ return ""; }; var test={ a:function(){ reurn ""; }; }; 他们的本质区别
[quote=引用 43 楼 weilimicrver 的回复:] [quote=引用 40 楼 lcf 的回复:] [quote=引用 38 楼 weilimicrver 的回复:] 我一直无法理解js中 function test(){ this.a=function(){ return ""; } } test.prototype.a=function(){ return ""; }; var test={ a:function(){ reurn ""; }; }; 他们的本质区别
function base () { }
test.prototype.a = function () {return "";}
function extended () {}
extended.prototype = new base();
var extObj1 = new extended();
var extObj2 = new extended();
alert (extObj1.a()); // ""
extObj1.a = function() { return "haha";}
alert (extObj1.a()); // "haha"
// here is the magic part
base.prototype.a = function() {return "hey, here is the magic!";}
alert(extObj2.a()); // "hey, here is the magic!"
alert(extObj1.a()); // "haha"
简单来说,新建对象的属性是prototype的属性的引用,改变了prototype对象,新建的对象也被改变,并且这种改变是可传递的。如果是function test() {
this.a = function() {return "";}
}
你就没有这种改变prototype的属性就影响到所有实例对象的便利了
好啦好啦我知道这里是JavaSE板[/quote]
貌似是这么这道理,谢了我一直无法理解js中 function test(){ this.a=function(){ return ""; } } test.prototype.a=function(){ return ""; }; var test={ a:function(){ reurn ""; }; }; 他们的本质区别