A Recommendation for interface-based programming

网友投稿 285 2022-09-20

A Recommendation for interface-based programming

When implementing custom data structures, it is best to keep methods such as forEach, find and findLast coherent with Javascript's interface standards. This is beneficial to the future upgrade of data structure, and also saves you time to remember different realization processes of the same interfaces. Taking forEach as an example [1]:

The forEach() method executes the provided callback once for each value which actually exists in the Set object. It is not invoked for values which have been deleted. However, it is executed for values which are present but have the value undefined. callback is invoked with three arguments: the element value the element key the Set object being traversed There are no keys in Set objects, however, so the first two arguments are both values contained in the Set. This is to make it consistent with other forEach() methods for Map and Array. If a thisArg parameter is provided to forEach(), it will be passed to callback when invoked, for use as its this value. Otherwise, the value undefined will be passed for use as its this value. The this value ultimately observable by callback is determined according to the usual rules for determining the this seen by a function. Each value is visited once, except in the case when it was deleted and re-added before forEach() has finished. callback is not invoked for values deleted before being visited. New values added before forEach() has finished will be visited. forEach() executes the callback function once for each element in the Set object; it does not return a value.

In this case, it is not hard to tell that interface-based programming is a time saver. Without interface-based programming, software architecture cannot be low in coupling and high in cohesion.

References

[1]. ​​MDN contributors​​. (Mar 28, 2022). Set.prototype.forEach(). MDN Web Docs. Title picture from: Recommendation for interface-based programming - AFFiNE Blog​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:国家卫健委:新增本土确诊病例124例 新增死亡病例1例!
下一篇:从新兵到隐形冠军,看数字化如何赢得韧性、效率与可持续发展的挑战
相关文章

 发表评论

暂时没有评论,来抢沙发吧~