Using prototypes, you need to copy the values of the array and duplicate them into a new array.
That is, there is[1,2]
need to[1,2].ocopy()
returned the result [1,2,1,2].
<script type="text/javascript"> var co = function() { var nor = [], count = Object.keys(this).length; for(var i = 0; i < count; i++) { nor.push(this[i]); nor.push(this[i]); } return nor; } Object.prototype.ocopy = co(); console.log([1,2].ocopy()); </script>
In this form, the code does not work at all, and this in the co () function generally understands that we are accessing the window.
Where is the mistake?
this
it has nothing to do with copying elements. - Dmitriy SimushevObject.prototype.ocopy = co();
- you assign the result of the function, i.e.count
, not the function itself. - etki[1,2].ocopy()
should return 2, and returns '[1,2] .ocopy is not a function' - mix2
?! according to the conditions of the problem, he must return[1,2,1,2]
. You would have first decided what exactly you need. - Dmitriy Simushev