JS uses the prototype custom array method example

Before starting, first introduce the benefits of using the prototype definition method in JS

often at the front end interview Or communication with other peers is that the method of constructing the configuration in the JS definition constructor is to use the prototype: Define the method to the Prototype of the constructor, this is the example of generating the constructor The own method is to point to a function of a function, which saves memory.

And this article mainly introduces the relevant content of JS using the prototype custom array method, share it out for your reference to study, let’s take a look at it, let’s take a look at the detailed introduction.

How to achieve the following code:

[1, 2, 3, 4 , 5] .duplicator (); // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
   solve Method 
Use Array’s Prototype attribute, custom

Duplicator () method, JS code is as follows:

     Array.Prototype.Duplicator = function () {let s = this.concat (this) return s} let t = [1, 2, 3, 4, 5] .duplicator () console.log (t)              Object 
When writing this code

Array.Prototype.duplicator Be careful not to add any parentheses, the THIS inside the function refers to the object that calls this method, namely Array.

summarizes Js利用prototype自定义数组方法示例

The above is the full content of this article. This article also has many shortcomings. I hope this article has a certain reference value for everyone’s learning or work. If you have any questions, you can leave a message, thank you TUMI cloud support.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment