javascript - How to get the functionality of Object/Array .prototype without using prototype? -


OK, so I've blocked myself with a stupid move that is now using the jQuery library , And should say well to me, rather it is likely to break more than I have done after reading. Anyway I was trying to use the following bit:

  Array.prototype.contains = function (v) {for (var i = 0; i & lt; this.length; i ++) {if (this [i] === v) Returns the truth; } return false; }; Array.prototype.unique = function () {var arr = []; (Var i = 0; i & lt; this.length; i ++) {if (! Arr.contains (this [i])) {arr.push (this [i]); }} Return arr; }   

However, to achieve the unique values ​​of an array, it ended up breaking a lot of things due to the conflict for jQuery in my reason, so far I have been tasty To use the prototype forbidden idea? Especially in this case of unique values ​​required in an array?

A common way to avoid modifying the prototype is the native type of static method:

  Array.unique = function (unit) {// your stuff}; // Example call var unique = Array.unique ([1, 1, 2, 3]);   

Or, to take this one step further, do anything like this

  var arrays = arrays || {}; Arrays.unique = function (unit) {/ * รข ?? | * /};   

In this way you are completely separated from the underlying array .

Comments

Popular posts from this blog

jasper reports - How to center align barcode using jasperreports and barcode4j -

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -