Say there is an object called obj
, and it has an attribute named attr
.(假设有一个名为obj
,它有一个名为attr
。)
In some function body I access the attribute say three times using a syntax like this ( obj.attr
).(在某些函数体中,我使用类似这样的语法( obj.attr
)访问属性说了三遍。) Is there a disadvantage to declaring a local variable const objAttr = obj.attr
, and using it in place of obj.attr
?(声明局部变量const objAttr = obj.attr
并使用它代替obj.attr
是否有缺点?) I would declare the local variable to avoid the cost of repeatedly accessing attr
from its obj
.(我将声明局部变量,以避免从obj
重复访问attr
的开销。) But I am wondering if there might be a disadvantage to creating the local variable for the mere sake of avoiding repeated access calls?(但是我想知道,仅仅为了避免重复访问而创建局部变量是否有不利之处?)
ask by faint-hearted-fool translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…