gpt4 book ai didi

javascript php中当前和下一个函数的等价物是什么?

转载 作者:行者123 更新时间:2023-11-30 10:13:50 24 4
gpt4 key购买 nike

我想在 Javascript 中获取当前项目并在其中处理后移动数组指针
就像 php 函数 current() & next()

有点像

array.current()
array.next()

有什么帮助吗?

最佳答案

制作原型(prototype)函数。例如

    Array.prototype.cursorPosition = 0;
Array.prototype.current = function(){
return this[this.cursorPosition];
}
Array.prototype.next = function(){
this.cursorPosition=this.cursorPosition+1;
return this[this.cursorPosition];

}
Array.prototype.previous = function(){
this.cursorPosition=this.cursorPosition-1;
return this[this.cursorPosition];

}

// implementation
var fruits = ["Banana", "Orange", "Apple", "Mango"];
alert(fruits.current()) ;
alert(fruits.next()) ;
alert(fruits.previous()) ;

关于javascript php中当前和下一个函数的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25035037/

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