mrsagar105
mrsagar105

Reputation: 204

Is there a way to access all the values of objects inside the prototype function?

I'm learning javascript and I came across this situation, that I am not able to figure out by myself.

Let's say I created a constructor function like this which is trying to mimic an array:

// 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');

This will create Object fruits like this:

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

Now, I want to create my own function for myArray that can access all the values of the fruits object so I can perform some operations with it. Like, reverse function for example:

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

I can set this.args = arguments; inside the constructor function and access args inside the prototype function to do something with it.

But it is only limited to the original Object. If I add more elements to my object in the future by using another function, then it won't work as it is only taking args from the original object. Is there a way to pass the current Object inside the prototype function?

EDIT:
As Tushar Shahi mentioned, we can implement reverse function by reversing the original argument. But how can we access the elements that are added to the object later. Here is the full code just so you guys can have a clear understanding.

// 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; }

As you can see in the last console.log, I am able to reverse only 3 arguments that were passed originally but not the elements that were added later.
Pineapple should be the first fruit after reversing. So the question is
How can we access elements added later inside the reverse function? or
How can we access the current fruit object inside the reverse function?

Upvotes: 2

Views: 979

Answers (2)

Peter Seliger
Peter Seliger

Reputation: 13431

This code does not answer the OP's original question. It gets provided just in order to show to the OP, how a simple Stack abstraction actually could look like (something the OP's implementation does mimic which I was referring to in my last comment to the 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; }

Upvotes: 1

Tushar Shahi
Tushar Shahi

Reputation: 20616

If you keep a copy of your initial arguments as an array it should work.

arguments is an array-like object, not exactly an array. So that has to be converted. Also, do not forget to reverse the args property itself. So it works when you use it multiple times.

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();

Upvotes: 1

Related Questions