gpt4 book ai didi

javascript - 扩展 javascript native 对象

转载 作者:行者123 更新时间:2023-11-30 08:51:07 24 4
gpt4 key购买 nike

<分区>

我有这个代码:

pPoint = function(x,y){
this.x = x || 0;
this.y = y || 0;
}

pPoint.prototype = {
constructor:pPoint,
add:function(){
return this.x+this.y;
}
}

如果我这样做:

a = new pPoint(10,20)
console.log(a.add());

按预期工作(返回 30)。

但是如果我这样做:

Array.prototype = {
abcd:function(){
console.log("bla bla testing");
}
}

然后这样做:

b = new Array();
b.abcd();

它不起作用...为什么?

我知道如果我这样做会很好...

Array.prototype.abcd:function(){
console.log("bla bla testing");
}
}

我只是不明白为什么前面的那个适用于我的 pPoint 而不是阵列...

fiddle :http://jsfiddle.net/paulocoelho/wBzhk/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com