gpt4 book ai didi

javascript - 我需要创建一个 Javascript 函数来创建自定义推送功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:33 24 4
gpt4 key购买 nike

我正在构建 JavaScript 代码来制作自定义推送功能。我的新功能应该与原始推送功能完全一样。

这是代码。请检查一下。

<script type="text/javascript">

function MyArray(){
this.add=function(x){

return this[this.length]=x;
}
}

MyArray.prototype=Array.prototype;

var collection = new MyArray();


collection.push(44);

collection.add(56); // this is not working.

collection.push(77);
collection.push(88);

console.log(collection);

</script>

最佳答案

因为您没有使用 native 数组,length 属性不会自动调整自身。您需要手动增加它,否则下一次 push 只会覆盖它:

function MyArray(){
this.add=function(x){

return this[this.length++]=x;
}
}

关于javascript - 我需要创建一个 Javascript 函数来创建自定义推送功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27468069/

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