0

I've never worked with prototype, and maybe I don't even understand how it works, so here I am.

I'm trying to change how .push() works in javascript: it adds the parameter as the last element, but I want it to add the parameter in the first position.

Here is my non-working guess:

    Array.prototype.push() = function(e){
      var x = [e]; 
      return x.concat(this);
    }

Sorry for the breathtaking mistakes I probably made :)

Disclaimer: I'm just trying to understand how to modify native methods. Why is everybody so scared?

2 Answers2

1

Just loose the parenthesis :

Array.prototype.push = function(e){
  var x = [e]; 
  this.unshift(e);
}

EDIT:

If you don't want to use unshift inside, you can do splice :

 Array.prototype.push = function(e){
   var x = [e];   
var y = [];
   for(var j = 0; j< this.length; j++) y[j] = this[j];
   this[0] = x;
   for(var i = 0; i < y.length; i++) this[i+1] = y[i];
}
binariedMe
  • 4,309
  • 1
  • 18
  • 34
  • I don't think all browsers support overriding native functions. – Prajwal Jun 10 '17 at 07:57
  • @Prajwal Well, I don't think so. Can you please give an example of browser? – binariedMe Jun 10 '17 at 07:58
  • 2
    This code will return a new array rather than modifying the array it is called on. – nnnnnn Jun 10 '17 at 07:59
  • @binariedM thank but i cant make it work. The console says: "Uncaught ReferenceError: Invalid left-hand side in assignment" in the line of "this = x.concat..." – Jose Luis Dominguez Quintans Jun 10 '17 at 08:08
  • I never seen a scenario where you replace native function. Would this fall under good practice? – Prajwal Jun 10 '17 at 08:09
  • Actually the problem with prev. one was that you can't reassign this. – binariedMe Jun 10 '17 at 08:14
  • thanks @binariedMe but with your solution, deeply, your just using unshift, that im trying to avoid. im trying to edit somehow the array "manually" – Jose Luis Dominguez Quintans Jun 10 '17 at 08:22
  • @JoseLuisDominguezQuintans The edit section is using just length property of array which too can be avoided using an undefined check in while loop. Try that. – binariedMe Jun 10 '17 at 08:38
  • 1
    You don't need the temporary `y` array, you can slide the existing elements to the right with one loop something like `for(var i = this.length; i > 0; i--) this[i] = this[i-1]`. (With a slight adjustment you can slide the elements over by more than one space, should you want to insert multiple elements at the beginning.) – nnnnnn Jun 10 '17 at 09:03
  • @Prajwal in javascript, you can almost hack the entire language, which is a powerful feature, but dangerous. most of the time, if not always, changing on the prototype of the native constructors is considered bad practice (such as editing the `push`) because if your code depend on libraries etc, they expect their push to work like it should, not like how you reimplemented it. – Bamieh Jun 10 '17 at 09:05
  • @Prajwal Its not a bad practice if you have a good reason. Like lets say, I want a log to be triggered whenever someone pushes something to an array. – binariedMe Jun 10 '17 at 09:07
1

To add element in first position use .unshift. You don't need prototype for this.

You don't want to override .push() or any other native method.

var data = [1,2,3,4,5];
data.unshift(10);

// 10 is first element
console.log(data);
loelsonk
  • 3,570
  • 2
  • 16
  • 23