我在調用另一個函數,並都具有相同名稱的組件的功能。
它的作品,運行的功能
我想知道爲什麼它工作(見我的假設下面)? 一個後續問題,如果我想遞歸調用外部函數我應該使用this.myMethod()
?
此模式是否安全使用?呼叫在道具的方法的函數內使用相同的名稱
實施例:
myMethod(){
const {myMethod} = this.props;
if(typeof myMethod == 'function'){ // i was sure this will check the external myMethod func
myMethod(); // i was sure it will call the external myMethod func.
}
// what if i want to call to the external "myMethod" func (recursively)?
}
我假定發動機正在搜索名爲myMethod
的外部myMethod
(詞法作用域?)體內的功能和它發現它。
任何人都可以證實這個假設?
編輯
只是爲了讓我的問題如何共同安全的這種模式更加清晰。
比方說,我有Item
一個組成部分,它有一個onTouch
道具PropTypes.func
,我想經營自己的函數裏面的一些內部邏輯,然後運行通過props
作爲回調或類似的東西收到的功能。
我應該周折才找到一個新的名字,以我的內部功能類似onInternalTouch
或者是它的安全堅持使用相同的名稱onTouch
?
onTouch(){ // is this common practice?
const {onTouch} = this.props;
// ... internal component logic
onTouch();
}
onInternalTouch(){ // or should i bother finding a new name?
const {onTouch} = this.props;
// ... internal component logic
onTouch();
}
好吧,你基本上重複了我的邏輯片段。如果我正確理解你,爲了調用「父」方法'遞歸地我需要在它自己內部使用'this.myMethod'(外部'myMethod') –
好吧,剛剛看到你的編輯:) –
這是一種常見的做法嗎? (在反應中使用道具) –