1

How can we update passed object in prototype? I have created similar prototype as Array.reverse, but how I can modify original object?

Array.prototype.myReverse = function() {
  let arr = [];
  for (let i = 0; i < this.length; i++) {
    arr.unshift(this[i]);
  }
  return arr;
}

let a = [9, 0, 3, 4];
console.log("Before ", a); // [9, 0, 3, 4]
console.log("reverse - ", a.myReverse()); // [4, 3, 0, 9]
//not modifying original object , how to modify original object
console.log("After ", a); // [9, 0, 3, 4]

I have check few examples but I didnt get how to update orginal object inside prototype How can we crate a prototype which will update original object (Careful: reverse is destructive -- it changes the original array.) If not possible for predefined Array then how we can create similar MyArray to write a prototype for updating original object.

3
  • 3
    Modify the properties of this. Commented Jan 6, 2020 at 13:54
  • @Pointy , Thaks. I am getting error while reassigning this object with equal operator.. I tried removing and addeing all element. I will update in answer Commented Jan 6, 2020 at 14:04
  • 1
    I said modify the properties of this. You cannot assign a value to this. Commented Jan 6, 2020 at 14:05

3 Answers 3

2

You cannot assign this directly, but you can still change its properties. So keeping in the style of the code you posted, you could do something along the lines of:

Array.prototype.myReverse = function() {
  let arr = [...this]
  for (let i = 0; i < this.length; i++) {
    this[i] = arr.pop()
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to reverse the array in-place (and return it if you would like), you can create a temporary stack by popping the head of the array until it is empty and then pushing the temporary elements as if they were in a queue.

  1. To temp:
    • ARR→POP ⇒ TMP→PUSH (LILO)
    • ARR→SHIFT ⇒ TMP→UNSHIFT (FIFO)
  2. From temp:
    • TMP→POP ⇒ ARR→UNSHIFT (LOFI)
    • TMP→SHIFT ⇒ ARR→PUSH (FOLI)

Where ARR is the self-reference array.

if (Array.prototype.reverseItems === undefined) {
  Array.prototype.reverseItems = function() {
    let tmp = []
    while (this.length > 0) tmp.push(this.pop())    // or `tmp.unshift(this.shift()`
    while (tmp.length  > 0) this.unshift(tmp.pop()) // or `this.push(tmp.shift())`
    return this
  }
}

let original = [ 9, 0, 3, 4 ]
original.reverseItems() // in-place
console.log('Reversed:', original.join(','))

Comments

-1

@pointy Thanks for suggestion..

I have modified properties of this and updated original object

Array.prototype.myReverse = function () {
 let arr = this.slice(); // creating a copy of array
  this.splice(0,this.length); // removing all elements from array
  for(let i = 0; i<arr.length;i++){
    this.unshift(arr[i]);
  }
  return this;
}

let a = [9,0,3,4];

console.log("Before ",a);
console.log("reverse - ", a.myReverse());
console.log("After ", a);

Few other links for native imlimatation of exsiting array prototypes

https://medium.com/@ofirrifo/naive-implementation-of-js-array-methods-a56319cad6b8

https://gist.github.com/alexhawkins/28aaf610a3e76d8b8264

Node.js change Number object value inside prototype

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.