Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

js: return this 无用的数据是否影响性能?

为了链表(类似sql的curd)操作, 往往需要return this.

项目中需要return this的概率为30-50%.

也就是说:
A: 出现概率70%
this.xy()
B: 出现概率30%
this.xy().angle()
AB:
var a = this.xy()
a.angle()

请问该不该return this;


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

1 Answer

0 votes
by (71.8m points)

影响不大,代码还是以大家用着都舒适为要,有句话叫“过早优化是万恶之源”嘛。
隔壁 jQuery一条语句就浓缩了选择器解析、方法重载、DOM接口、隐式迭代等费事的流程,最后还返回了 this 来支持链式操作,但用起来效率还行。
当然,实在担心的话你可以写两种接口,一种有固定前缀的不返回,另一种有返回。。。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...