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
?
views:
253answers:
3
+1
A:
Just in case you have a getTracker() function in your own code, or similar.
In other words, to avoid naming conflicts with the page's javascript code, probably.
@Theo: Didn't realize (ie, not read carefully enough) they were methods. Then maybe to encourage caution or discourage use? Dunno, really.
Vinko Vrsalovic
2008-09-12 16:37:53
Those are methods, there's no possibility of naming conflicts unless I start declaring methods on the page tracker's prototype.
Theo
2008-09-12 16:45:24
A:
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.
scunliffe
2008-09-12 17:01:19
I take it you haven't used Google Analytics? *All* methods on the page tracker object are prefixed with an underscore, even those that explicitly stated as public in the documentation.
Theo
2008-09-12 17:55:46
+1
A:
Because Google can't be bothered to follow the Module Pattern and therefore they don't want accidental collisions in the global namespace?
Hank Gay
2008-09-12 19:05:42