Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

javascript - 将可重复访问的对象属性保存在局部变量中是否有缺点? [关闭](Is there a disadvantage to holding a repeatedly accessed object attribute in a local variable? [closed])

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...