I use js, jquery

Wrote the function contains(object, item, showCount) , which shows whether the item element is in the string or the object array, and if necessary it returns the number of repetitions of this element, getting showCount = true .

And suddenly I wanted to call something like this: Array.contains(item, showCount) , or so String.contains(item, showCount) .

Is it possible to call her like that? If there is, how?)

  • one
    Of course there is an opportunity. you just have to ... - Grundy
  • Array.prototype.yourFunctionName = function () {// your code} - Vladislav Siroshtan

1 answer 1

 function myFunction() { for (var i = 0; i < this.length; i++) { console.log(this[i]); } } Array.prototype.myFunction = myFunction; var arr = [1, 2, 3, 4, 5]; arr.myFunction(); 

  • Thank you, and with the string as it happens? - Mr Klonwar
  • It turns out, if to alter function a little - Cheg