admin管理员组文章数量:1277336
As several developers work on the javascript files , many of them ended up writing the same file names again and again
simple example can be getCookie , setCookie type of functions.
Now we are doing aggregation on javascript files , will there be any problem if have same functions twice.
Right now things are working fine , but I want to know
Appreciate your help
As several developers work on the javascript files , many of them ended up writing the same file names again and again
simple example can be getCookie , setCookie type of functions.
Now we are doing aggregation on javascript files , will there be any problem if have same functions twice.
Right now things are working fine , but I want to know
Appreciate your help
Share Improve this question edited Nov 18, 2010 at 20:00 Daniel Vassallo 345k72 gold badges512 silver badges446 bronze badges asked Nov 18, 2010 at 19:54 kobekobe 15.8k15 gold badges65 silver badges93 bronze badges3 Answers
Reset to default 11Yes, there will definitely be a problem if you end up having a function unintentionally defined twice in the global namespace. The function that was last defined will simply overwrite the previous one.
You may want to consider using namespaces to tackle this problem. JavaScript namespaces are normally mimicked by using objects and closures, and often initialized with a self-invoking function:
var myNamespace = (function () {
var _name = 'Bob';
return {
somePublicMethod: function () {
return 'Hello, ' + _name;
}
};
})();
alert(myNamespace.somePublicMethod());
It's kind of like The Highlander — for any given global symbol, there can be only one!
Namespaces are part of the answer, the other part is not writing duplicate code.
The name of a function or variable should describe its purpose, so two functions with the same name should be the same function! You can't do this perfectly, of course, that's why we have namespaces.
本文标签: jqueryDuplicate JavaScript names Is it bad to have themStack Overflow
版权声明:本文标题:jquery - Duplicate JavaScript names. Is it bad to have them? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741213209a2359537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论