facebookarchive / react-meteor

React rendering for Meteor apps
948 stars 113 forks source link

a simple question #87

Open zzz6519003 opened 9 years ago

zzz6519003 commented 9 years ago
React = window.React;
delete window.React;

wondering what this does :beers:

froatsnook commented 9 years ago

@zzz6519003 Uhh, maybe some of this is wrong, but I'm pretty sure it works as follows.

In short:

  1. react.js assigns React to window
  2. React = window.React; makes a package-wide variable which is later exported
  3. delete window.React; removes the window variable "leak"

Most of this package defines React in: vendor/react-with-addons-0.13.0.js. This creates window.React.

Normally in Meteor apps:

// This is local to the current file
var ABC = function() { };

// This is globally available in the APP
XYZ = function() { };

But in packages:

// This is local to the current file
var ABC = function() { };

// This is globally available in the PACKAGE
XYZ = function() { };

In order to make a package global variables available in apps that include the package, you take one of these XYZ variables and export it in package.js.

So now to answer your question:

When you say:

XYZ = function() { }; // (i.e. without var)

It assigns to this, which normally happens to be window in the browser. But in a package, it is assigned to something else (for package-wide variables). Assigning React to window would be "bad" because if you didn't export it in package.js it would be visible by apps anyway.

zzz6519003 commented 9 years ago

so long story short, it just wanna make it clean, just a good habit? thx buddy! :beer: