Amit
Amit

Reputation: 323

Javascript prototype and modify original object

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.

Upvotes: 1

Views: 543

Answers (3)

Amit
Amit

Reputation: 323

@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

Upvotes: -1

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48683

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(','))

Upvotes: 0

Sheraff
Sheraff

Reputation: 6722

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

Upvotes: 2

Related Questions