How to get the global variable of an element to avoid being polluted?
曾经蜡笔没有小新2017-05-19 10:11:18
0
4
578
var ele=document.getElementById('btn'); When writing native js, you need to obtain a large number of page elements. Now that there are too many global variables, how do you avoid being polluted?
Open namespace:
var name={};
name.common={};
name.fx={};
name.site={};
name.common.ele1= document.getElementById('btn' );
neme.fx.ele1= document.getElementById('btn2');
Wrap it in IIFE and don’t put it globally.
Same upstairs, wrap one floor with IIFE
Modular. requirejs/webpack/ES6 modules, variables are all private to the module.