为什么Google Analytics跟踪代码中的所有方法都以下划线开头?

时间:2022-04-18 15:21:03

Prefixing variable and method names with an underscore is a common convention for marking things as private. Why does all the methods on the page tracker class in the Google Analytics tracking code (ga.js) start with an underscore, even the ones that are clearly public, like _getTracker and _trackPageView?

使用下划线对变量和方法名称进行前缀是将事物标记为私有的常见约定。为什么Google Analytics跟踪代码(ga.js)中的网页跟踪器类的所有方法都以下划线开头,即使是明显公开的那些,如_getTracker和_trackPageView?

3 个解决方案

#1


4  

Because Google can't be bothered to follow the Module Pattern and therefore they don't want accidental collisions in the global namespace?

因为Google无法遵循模块模式,因此他们不希望在全局命名空间中发生意外冲突?

#2


1  

Just in case you have a getTracker() function in your own code, or similar.

以防您在自己的代码中使用getTracker()函数或类似函数。

In other words, to avoid naming conflicts with the page's javascript code, probably.

换句话说,可能是为了避免与页面的javascript代码发生命名冲突。

@Theo: Didn't realize (ie, not read carefully enough) they were methods. Then maybe to encourage caution or discourage use? Dunno, really.

@Theo:没有意识到(即,没有仔细阅读)他们是方法。那么也许鼓励谨慎或不鼓励使用?不知道,真的。

#3


0  

I've always read this like so:

我总是这样读:

If the property/method is prefixed with an underscore, it is for some "internal" workings. Therefore if you are about to use/call/alter this property/method, you had better darn well know what you are doing, and or expect it to possibly be renamed/removed in a future release.

如果属性/方法以下划线为前缀,则表示某些“内部”工作。因此,如果您要使用/调用/更改此属性/方法,您最好知道自己在做什么,或者期望在将来的版本中重命名/删除它。

#1


4  

Because Google can't be bothered to follow the Module Pattern and therefore they don't want accidental collisions in the global namespace?

因为Google无法遵循模块模式,因此他们不希望在全局命名空间中发生意外冲突?

#2


1  

Just in case you have a getTracker() function in your own code, or similar.

以防您在自己的代码中使用getTracker()函数或类似函数。

In other words, to avoid naming conflicts with the page's javascript code, probably.

换句话说,可能是为了避免与页面的javascript代码发生命名冲突。

@Theo: Didn't realize (ie, not read carefully enough) they were methods. Then maybe to encourage caution or discourage use? Dunno, really.

@Theo:没有意识到(即,没有仔细阅读)他们是方法。那么也许鼓励谨慎或不鼓励使用?不知道,真的。

#3


0  

I've always read this like so:

我总是这样读:

If the property/method is prefixed with an underscore, it is for some "internal" workings. Therefore if you are about to use/call/alter this property/method, you had better darn well know what you are doing, and or expect it to possibly be renamed/removed in a future release.

如果属性/方法以下划线为前缀,则表示某些“内部”工作。因此,如果您要使用/调用/更改此属性/方法,您最好知道自己在做什么,或者期望在将来的版本中重命名/删除它。