在性能方面哪一个更好的方法是第一或第二?
const getCookieValue = readCookie('my_var')
应该在顶部声明,或者因为它的使用只是在一个条件下才能更好地保持在if
声明中
方法1
componentWillMount() {
const {data1, data2} = this.props
if(data1) {
const getCookieValue = readCookie('my_var')
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
要么
方法2
componentWillMount() {
const {data1, data2} = this.props
const getCookieValue = readCookie('my_var')
if(data1) {
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
性能明智 - 方法1,你回答了你的问题 - 因为它的使用只是在一个条件下更好地保持在里面