-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
暴露 window.detector 全局对象 #43
Comments
想了下,可以改造如下,就不用 detector 暴露全局对象了: // tracker.js
(function(global){
var detector;
var Tracker = function(){};
if("function" === typeof define){
define(function(require, exports, module){
detector = require("detector");
sendData(detector);
module.exports = Tracker;
})
}else{
detector = global.detector;
sendData(detector);
}
function sendData(detector){
send(detector);
}
})(this); 不过如果暴露全局对象,像浏览器升级提示这种需求,就不用这么麻烦了。 |
直接暴露的问题还是「冲突隐患」,因为 detector、tracker、monitor 这些脚本,长久来说,如果发展得好,很可能部署到整个集团,甚至外部网站也能直接用。这些情况下,暴露的全局变量名,就很关键。 比如 google 分析的部署脚本,只占用 <script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36247332-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
if (location.port) {
return;
}
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script> 从保持简单性上考虑,我的建议:
sea.js 可以考虑修改成这种方式(还需要进一步讨论确定)
|
OK, 收到。 求取名。
|
取名是最头疼的事…… |
是不是反了? 按我理解应该是 require('detector') 时, 应先判断 window['detector'] 是否存在, 否的话再 require('detector') |
没反,乔花。局部优先,没有时才到全局看一下。和作用域一样的。 On Mon, Mar 3, 2014 at 2:27 PM, Yan notifications@github.com wrote:
王保平 / 玉伯(射雕) |
别跑题,快想名字~ |
_spy |
g_detector ? g 是 global 的意思。甚至后续可以约定,我们的模块,如果需要往全局暴露变量,都遵守 g_lowercase_lowercase 这种格式。 On Mon, Mar 3, 2014 at 10:01 PM, Yan notifications@github.com wrote:
王保平 / 玉伯(射雕) |
@lifesinger 不好看 |
南伯给个好看的建议 On Tue, Mar 4, 2014 at 9:10 AM, Hsiaoming Yang notifications@github.comwrote:
王保平 / 玉伯(射雕) |
tan.js (探.js) 如何?短,好寫,好記。 |
不错。 不过这里讨论的不是模块名称,而是暴露的全局变量名,window.tan 或 window.Tan 冲突的概率大不大? |
@hotoo 我覺得不大。應該沒人用這個名字。如果覺得有的話,可以用 _tan 呀。 |
那考虑 |
不过话说放弃 detector 的名字有点不舍~ |
经组织讨论决定,为了规范统一,组件名称保持 detector 不变,暴露的全局变量使用 |
这两句 |
之前在非 cmd 环境已经暴露了 detector,所以保留了这个。 |
detector 代码本身不再暴露全局变量,如有需要,可以使用 zpm 或者自行定制编译。 |
The text was updated successfully, but these errors were encountered: