Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is continuation of this question but for ARRAYS. Suppose I want to call array function with 2 parameters e.g.

console.log(
  [3,4,5].slice(1,2)
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
248 views
Welcome To Ask or Share your Answers For Others

1 Answer

The existing technique to wrap the subject of the method inside an array, which is mentioned here, was intended specifically for strings, turning "string" into ["string"]. The trick was using .split() without arguments. After applying that trick, it was not so difficult to use the other little tricks to apply the method that was desired, and allow chaining on the result.

Of course, now that the subject is an array, we cannot use .split().

It turns out that you can use the following method to make it happen for arrays:

console.log(
  [3, 4, 5].reduce([].constructor).slice(-1)
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...