簡體   English   中英

如何確定使用“調用”時鏈接函數執行的方式?

[英]How to determine which way chained functions execute when “call” is used?

因此,例如,在Array.prototype.slice.call(arguments)callslice之前進行,但在var standard= name.substring(1).toLowerCase() substringtoLowerCase之前被調用。 為什么會有這種區別?在鏈接它們時,我如何知道調用哪個順序函數?

我認為您誤會了一些東西...

Array.prototype.slice.call(arguments)

僅調用一個函數,它是slice()

.call()調用slice()函數, .call()slice的方法

所以

call()使用提供的上下文和參數調用先前的方法 這使其從右到左直到只有call()之前的上一個函數

以及關於鏈接

鏈接 只是意味着您使用調用的函數的返回值,並且該值是包含再次調用的方法的對象。 所以它從左到右發生

最好的例子是

var firststr = "my house".substring(2); //then this returns a string
var secondstr = firststr.trim(); //method call trim() on the return of .substring()

可以重寫為

"my house".substring(2).trim();

必讀:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

在您的示例中, prototypesliceArray對象的屬性:

Array.prototype.slice  // On the Array.prototype object, get `.slice` property

然后,用.slice財產,獲得財產.call上並執行。 在此示例中,只有.call()是方法調用(例如,函數調用)。 這根本不是鏈接。 這只是嵌套的屬性聲明,而不是鏈接。

像其他示例一樣進行鏈接:

var standard = name.substring(1).toLowerCase()

從左到右執行。 name.substring(1)被執行,並從該方法返回的結果上調用.toLowerCase()方法。 僅當先前方法返回適用於后續方法的對象類型時,鏈接才起作用。 在這種情況下,名稱返回一個新的字符串對象name.substring(1)並在該新字符串對象上調用.toLowerCase() 因此,事情從左到右執行。

sliceArray.prototype一個屬性(恰好是一個函數),並且您正在使用call來調用該函數(即一個函數調用)。 name.substring(1).toLowerCase()鏈接函數substringtoLowerCasename.substring(1).toLowerCase()順序)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM