Sunday 15 May 2011

javascript - How do I change the Array in place when prototyping -


I am writing a custom sort function that I am prototyping in the array. (Please do not post the replies given to me that I should not bother the prototype in the array for any reason because you think the prototype in the array is not a good idea).

So, my method looks like this:

  // method Array.prototype.mySort = function (memberName, ascOrDesc) {var label rau = this.shift ( ); Var ret = this.sort ((function (a, b) {if (ascOrDesc> 0) returns (a [username]> gt [username]] 1: -1; return (one [username] & lt; b [Subscriber]) 1: -1;})); Ret.unshift (labelRow) Return Rit; }   

Note that this is how to do this. Shift () will affect the array at PLACE

However, I'm not clear how I've completed it. If I want to write my own myShift method, at some point I will need to say something about the effect of <= p>

  this = this.myShift ();   

Which is clearly illegal.

So, I am trying to understand how change () changes the members of the array and is able to remove one in the first place - Space And if I have permission to do something similar, or it has been baked somehow and is not available to use me.

You can access the array inside the method using this .

For example, you can apply shift method:

  Array.prototype.myShift = function () {If (this.length == 0) return tap ; Var result = it [0]; (Var i = 1; i & lt; this.length; i ++) for {this [i-1] = this [i]; } This.length--; Return result; };    

No comments:

Post a Comment