[英]Bind every child function's context to parent function
假設我有一個父 function 調用一些這樣的子函數:
function parentFn() { API.greet(); } parentFn.name = 'john'; const API = { greet: function() { console.log(`Hi there ${parentFunc.name}`); // prints out 'Hi there undefined' } }; parentFn()
無論如何,在不向內部 function 添加參數的情況下,是否可以在 API 中獲取父級 function 的屬性?
有兩種情況:
parentFn.name
。 請注意,如果您不想聲明顯式參數, this
可以使用每個function
和方法都有的隱式參數。 It normally points to the API
object in a API.greet()
call, but you can change it like API.greet.call(parentFn)
(although this is slightly weird).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.