I've been trying to understand Samy Kamkar's evercookie code a开发者_高级运维nd he does at least one thing I simply don't understand. The relevant code can be seen online at http://code.google.com/p/beef/source/browse/trunk/modules/beefjs/lib/evercookie.js?spec=svn604&r=604.
My question is about how this code sets evercookie
itself. The relevant construct:
var evercookie = (function () {
this._class = function() {
...
};
return _class;
})();
I understand that evercookie
is intended to be a constructor (despite not following the convention of capitalizing constructors) and I understand that evercookie
is being set to the return of an anonymous, self-executing function. Unless I am more confused than I think I am, evercookie
should be set to the value _class
has in the scope of the outer (anonymous) function. What I don't follow: where does _class
get a value? _class
is not the same thing as this._class
, and there is no other reference to _class
.
In that function _class
is the same as this._class
because this
references the window object in the automatically executed function. It's essentially making the _class
function a global variable.
Here's the basic idea: http://jsfiddle.net/hYQab/
精彩评论