Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - Overwrite 'this' in function

I am trying to extend the Array prototype:

Array.prototype.rotate = function() {
  var arr = [];
  for (var i = 0; i < this[0].length; ++i) {
    arr[i] = [];
    for (var j = 0; j < this.length; ++j) {
      arr[i].push(this[j][i])
    }
  }
  this = arr;
  return arr;
}

Totally dandy, until this = arr. That bombs out.

How do I re-assign the this property of a prototypal function? I want the hells to do with the previous array configuration.

EDIT

Why am I doing it this way? I want it to behave like other array functions. For example, this works:

myArray.pop();

I don't need to do this:

myArray = myArray.pop();

ANOTHER EDIT

I did this to solve it, but it seems stupid:

Array.prototype.rotate = function() 
{
  var arr = [];
  var len = this[0].length;
  for (var i = 0; i < len; ++i) {
    arr[i] = [];
    for (var j = 0; j < this.length; ++j) {
      arr[i].push(this[j][i])
    }
  }
  for (var i = 0; i < this.length; ++i) {
    delete this[i];
  }
  for (var i = 0; i < arr.length; ++i) {
    this[i] = arr[i];
  }
  return arr;
}

This would work, but, in an example, when rotating this array:

[[1, 1], [2, 2], [3, 3]]

I would get:

[[1, 2, 3], [1, 2, 3], ]

See that little blank third item? Yeah - that caused me problems.

like image 733
dthree Avatar asked May 06 '14 23:05

dthree


People also ask

Can I override function in JavaScript?

Introduction. It is true that JavaScript supports overriding, not overloading. When you define multiple functions that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed.

How do you overwrite a function?

To override a function you must have the same signature in child class. By signature I mean the data type and sequence of parameters. Here we don't have any parameter in the parent function so we didn't use any parameter in the child function.

Which keyword is used to over ride the functions already existing in JavaScript?

Use the super Keyword in Inheritance to Get Back Previous Function. The only way to get back the previous function once we override it is to use the super keyword. We will create two functions with the same name and parameters, one in the parent class and one in the child class.

How do I stop JavaScript overriding?

This is the compatibility table for the Object. freeze method: kangax.github.io/compat-table/es5/#test-Object. freeze and this is the table for the Objact. defineProperty: kangax.github.io/compat-table/es5/#test-Object.


1 Answers

Although you cannot overwrite this, you can most certainly set the value of the working array to the array you've built out in your function. Something like this:

Array.prototype.rotate = function() {
  var arr = [];
  for (var i = 0; i < this[0].length; ++i) {
    arr[i] = [];
    for (var j = 0; j < this.length; ++j) {
      arr[i].push(this[j][i])
    }
  }
  this.length = 0; //empty the array
  this.push.apply(this, arr); //push all of the elements from arr onto this
  return arr;
}
like image 192
Sean Johnson Avatar answered Nov 08 '22 09:11

Sean Johnson