成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Easier Way to Understand apply and call in JS

Wildcard / 2959人閱讀

The first time I know apply was when I met this code:

Math.max.apply(null, [1, 2, 3, 4])

As the mdn shows, the syntax is:

function.apply( thisArg , [argsArray] )

Actually, in case above, thisArg has no influence which means code below also works:

Math.max.apply(undefined, [1, 2, 3, 4])
Math.max.apply(Math, [1, 2, 3, 4])

The only effect of apply in the code above is that it can pass the values in array to the function max. So, code above equal

Math.max(1, 2, 3, 4)

Why would I mention this? Because we don"t need this anymore because we already have ... which works like:

Math.max(...[1, 2, 3, 4])

The reason that we still need apply and call is the thisArg. They can help us call some powerful methods.

thisArg in apply and call

I guess you might have seen this code:

Array.prototype.slice.call({ length: 2 })
function fn() {
  console.log(Array.prototype.slice.call(arguments))
}
fn(1, 2, 3, 4) //[1,2,3,4]

Today, we don"t need this either because of Array.from. But I still want to talk about it for explanation. In the case above, call was used because we want to do something like:

let obj = { length: 2 }
obj.slice() //Uncaught TypeError: obj.slice is not a function

It would cause error because slice was defined in Array.prototype. Only Array instance can call that method. But actually in the implementation of slice, it doesn"t need to be called by Array instance and there is a lot of methods like this. So, in this case, call or apply would let non Array instance call these methods which means

Array.prototype.slice.call({ length: 2 })
//help you do
let obj = { length: 2 }
obj.slice = Array.prototype.slice
obj.slice()

And to help it easier to understand , you can remember it like:

method.call(thisArg, ...args)
//works like in most cases
thisArg.method = method
thisArg.method(...args)
//for apply
method.apply(thisArg, args)
//works like in most cases
thisArg.method = method
thisArg.method(...args)

Wasn"t that easy ?

So, let get back to Math.max.apply({}, [1, 2, 3, 4]). You can remember it like:

let thisArg = {}
thisArg.max = Math.max
thisArg.max(...[1, 2, 3, 4])

And more cases:

Object.prototype.toString.call([]) //"[object Array]"
//help you do this
let thisArg = []
thisArg.toString = Object.prototype.toString
thisArg.toString() //"[object Array]"
//while
[].toString()//""

Or

;[" sd ", 1, 3].map(Function.prototype.call, String.prototype.trim) //["sd","1","3"]
//help you do
;[" sd ", 1, 3].map(function(...args) {
  return String.prototype.trim.call(...args)
})
//help you do
;[" sd ", 1, 3].map(function(...args) {
  let thisArg = args[0]
  thisArg.trim = String.prototype.trim
  return thisArg.trim(...args.slice(1)) //Uncaught TypeError: thisArg.trim is not a function
})

In the case above, it will got error because args[0] is Primitive values. You can"t call methods in Primitive values. But it can still help you understand.

More in apply

As apply can accept an array-like object. So, what would happen if coding like:

Array.apply(null, { length: 2 })

Actually, it equals

Array.apply(null, [undefined, undefined])

So, you can understand it like:

let thisArg = {} //set null would get error in code below, also thisArg in above case is not important
thisArg.Array = Array
thisArg.Array(undefined, undefined)

Hope it"s easier to understand apply and call.

Original Post

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/99780.html

相關(guān)文章

  • The Power of Ten – Rules for Developing Safety Cri

    摘要:探測(cè)器的代碼就是寫的,真厲害 New Horizon 探測(cè)器的代碼就是 JPL 寫的,真厲害 http://pixelscommander.com/wp-content/uploads/2014/12/P10.pdf Gerard J. Holzmann NASA/JPL Laboratory for Reliable Software Pasadena, CA 91109 Mo...

    Muninn 評(píng)論0 收藏0
  • promise, async, await, execution order

    摘要: async can be transformed to promise. So, if we want to understand async, we have to understand promise first. Promise Normally, promise is easy to understand, especially when using like this: p...

    neuSnail 評(píng)論0 收藏0
  • 《JavaScript Web應(yīng)用開發(fā)》作者Nicolas:恰巧,愛好變職業(yè)(圖靈訪談)

    摘要:本文僅用于學(xué)習(xí)和交流目的,不得用于商業(yè)目的。今年,我們依然會(huì)組織。隨著語言的發(fā)展,這種情況將不再適用。本系列主要討論如何獲得這些高度模塊化的應(yīng)用程序。這一系列內(nèi)的后續(xù)圖書會(huì)討論測(cè)試及部署等內(nèi)容。更多精彩,加入圖靈訪談微信 本文僅用于學(xué)習(xí)和交流目的,不得用于商業(yè)目的。非商業(yè)轉(zhuǎn)載請(qǐng)注明作譯者、出處,并保留本文的原始鏈接:http://www.ituring.com.cn/art... 訪談...

    wawor4827 評(píng)論0 收藏0
  • 5 ways to find code online

    5 Ways to find code online In 2015 google and the university of Nebraska published a research titled How developers search for code: A case study. This research was conducted on real google developers...

    _ipo 評(píng)論0 收藏0
  • Life of a Pixel 2018

    摘要:將操作記錄為一列。在列表上進(jìn)行操作被稱為光柵化。能夠運(yùn)行產(chǎn)生位圖的命令加速光柵化注意到此時(shí)像素并不在屏幕上。因此,線程將劃分為。根據(jù)與的距離確定的優(yōu)先級(jí)。被包裝在一個(gè)中,該對(duì)象被提交給瀏覽器進(jìn)程。 This talk is about how Chrome turns web content into pixels. The entire process is called rend...

    Miracle 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<