Google分析跟踪代码使用的技术是什么? [英] What's the technique that the Google analytics tracking code uses?

查看:141
本文介绍了Google分析跟踪代码使用的技术是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Analytics跟踪代码如下所示:

 (function(){
code
})();

这些方括号使用的技术是什么 - (stuff)() - 叫?它是做什么的?



我把Google Analytics代码放在我的页面的闭头标签之前,然后像这样放置一条if语句(我包含一个Jquery cookie插件进一步提升):

 < script type =application / javascript> 
if($ .cookie('allowcookies')=='yes'){
analytics code
}
< / script>

直到我在代码中使用相同的技术时才运行:

 (function(){if($ .cookie('allowcookies')=='yes'){

分析代码
}
})();

为什么在我做之前没有运行?为什么它运行后?

解决方案

 (function(){
/ * code * /
}());

通常称为自我执行匿名函数(¹)(o«立即函数调用» ),它的主要用途是避免在全局(或外部)范围内创建变量。



当您想要创建函数时,它也被用作快捷键只需执行一次,而不需要先用自己的标识符定义函数,然后很快进行函数调用。

它可能最终在一个范围内使用,然后如果外部上下文(或其他引用)通过参数传递绑定,它可能会创建一个闭包,例如

  / *外部范围* / 
(函数(outerscope){

element.onsomeevent = function(){
/ *用outerscope * /
做些事情;

}(this));

这个表达式的另一个实际用途是当我需要创建一个函数,当使用 new 关键字调用构造函数时(而不是显式调用某些 init 方法) p>




(¹)—正如Nicholas Zakas在书Mantainable Javascript( O'Reilly,ISBN 978-1-449-32768-2 )page 44 中所述,建议的表达式是(function(){}()),嵌套parens(即使(function(){})()工作无论如何)


[...]为了使显而易见的立即函数调用发生,将paretheses em>

另请参阅立即函数调用语法


The Google Analytics tracking code looks like this:

(function() {
code
  })();

What's the technique they are using with those brackets - (stuff)() - called? What does it do?

I put the Google Analytics code before the closing head tag on my page and then put an if statement around it like this (I include a Jquery cookie plugin further up):

<script type="application/javascript">
if ($.cookie('allowcookies') == 'yes') {
analytics code
}
</script>

It didn't run until I used the same technique around my code:

(function() {if ($.cookie('allowcookies') == 'yes') {

analytics code
}
})();

Why did it not run before I did that? Why did it run after?

解决方案

(function() {
   /* code */
}()); 

It's commonly known as «self executed anonymous function (¹)» (o «immediate function invocation») and its main use is to avoid the creation of variables into the global (or in the outer) scope.

It's also used as shortcut when you want to create a function to execute just once, without the need to first define the function with its own identifier and then soon make the function call.

It may be eventually used inside a scope and then it may create a closure if the outer context (or other references) are binded through parameters passing, e.g.

/* outer scope */  
(function(outerscope) {

   element.onsomeevent = function() {
       /* do something with outerscope */
   };

}(this));

Another practical use I make with this expression is when I need to create a function to be soon executed inside a constructor function when it is called with new keyword (instead of an explicit call to some init method).


(¹) — as stated on book "Mantainable Javascript" by Nicholas Zakas (O'Reilly, ISBN 978-1-449-32768-2) page 44, the suggested expression is (function() {}()), with nested parens (even if (function() {})() will work anyway)

[...]To make it obvious that immediate function invocation is taking place, put paretheses around the function[...]

See also Immediate function invocation syntax

这篇关于Google分析跟踪代码使用的技术是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆