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?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(4)
phpcn_u1582

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

(function () {
    var foo = '...';
    var baar = '...';
})();
黄舟

Modular. requirejs/webpack/ES6 modules, variables are all private to the module.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template