有没有一种方法可以访问原型函数中对象的所有值



我正在学习javascript,遇到了这种情况,我自己无法解决。

假设我创建了一个类似这样的构造函数,它试图模拟一个数组:

// constructor function
function myArray() {
for (let i = 0; i < arguments.length; i++) {
this[`${i}`] = arguments[i];
}
this.length = arguments.length;
}
let fruits = new myArray('apple', 'mangoes', 'banana');

这将创建如下对象fruits

myArray {0: "apple", 1: "mangoes", 2: "banana", length: 3}
0: "apple"
1: "mangoes"
2: "banana"
length: 3

现在,我想为myArray创建我自己的函数,它可以访问fruits对象的所有值,这样我就可以用它执行一些操作。例如,reverse函数:

myArray.prototype.reverse = function () {
//how do i access all elements/values of friuts here?
};

我可以在构造函数中设置this.args = arguments;并访问原型函数内部的args来对其进行处理。

但它仅限于原始对象。如果我将来使用另一个函数向对象添加更多元素,那么它将无法工作,因为它只从原始对象中获取参数。有没有一种方法可以将当前Object传递到原型函数中?

编辑:
正如Tushar Shahi所提到的,我们可以通过反转原始参数来实现反转函数。但是我们以后如何访问添加到对象中的元素呢。这是完整的代码,只是为了让你们有一个清晰的理解。

// constructor function
function myArray() {
this.args = Array.from(arguments);
for (let i = 0; i < arguments.length; i++) {
this[`${i}`] = arguments[i];
}
this.length = arguments.length;
}
let fruits = new myArray("apple", "mangoes", "banana");
// reverse function
myArray.prototype.reverse = function () {
// reversing the arguments first then assigning values
this.args = this.args.reverse();
for (let i = 0; i < this.args.length; i++) {
this[i] = this.args[i];
}
// but how do i access all elements/values of friuts here and not just the arguments?
}
// add/push function
myArray.prototype.add = function (a) {
let index = this.length;
this[index] = a;
this.length++;
}
// pop function
myArray.prototype.pop = function (a) {
let index = this.length - 1;
delete this[index];
this.length--;
}
fruits.add("orange"); // orange is added to fruits
fruits.add("pineapple"); // pineapple is added to fruits
console.log('fruits.reverse() ...', fruits.reverse());
console.log({ fruits }); // myArray {0: "banana", 1: "mangoes", 2: "apple", 3: "orange", 4: "pineapple", args: Array(3), length: 5}
.as-console-wrapper { min-height: 100%!important; top: 0; }

正如您在上一个console.log中所看到的,我只能反转最初传递的3个参数,但不能反转后来添加的元素
菠萝应该是反转后的第一个水果。所以问题是
我们如何访问稍后在反向函数中添加的元素?或者
我们如何访问反向函数中的当前水果对象?

如果将初始参数的副本作为数组保存,它应该可以工作。

arguments是一个类似数组的对象,而不完全是一个数组。所以必须转换。另外,不要忘记反转args属性本身。因此,当你多次使用它时,它是有效的。

function myArray() {
this.args = Array.from(arguments);
for (let i = 0; i < arguments.length; i++) {
this[i] = arguments[i];
}
this.length = arguments.length;
}
let fruits = new myArray('apple', 'mangoes', 'banana');
myArray.prototype.reverse = function () {
this.args = this.args.reverse();
let j = 0;
for (let i = 0; i < this.args.length; i++) {
this[i] = this.args[i];
}
};
fruits.reverse();
fruits.reverse();

此代码不回答OP的原始问题。提供它只是为了向OP展示一个简单的Stack抽象实际上是什么样子(OP的实现确实模仿了我在对OP的最后一次评论中提到的东西)。。。

function Stack() {
const stack = Array.from(arguments);
this.push = (...args) => stack.push(...args);
this.pop = () => stack.pop();
this.size = () => stack.length;
this.reverse = () => Array.from(stack.reverse());
}
let fruits = new Stack("apple", "mango", "banana");
console.log(
'fruits.push("orange", "kiwi") ...',
fruits.push("orange", "kiwi")
);
console.log(
'fruits.push("pineapple") ...',
fruits.push("pineapple")
);
console.log(
'fruits.reverse() ...',
fruits.reverse()
);
console.log(
'fruits.pop() ...',
fruits.pop()
);
console.log(
'fruits.pop() ...',
fruits.pop()
);
console.log(
'fruits.size() ...',
fruits.size()
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

最新更新