Below we have an IIFE which (like any function) creates a local scope. Inside that scope there is a parseInt
function. Now, since there already is a global function in the browser with that name, the local function will overshadow the global parseInt
function - inside the IIFE any call to parseInt
will call the local function, and not the global one. (The global function can still be referenced with window.parseInt
.)
parseInt('123', 10); // the browser function is called
(function() {
function parseInt() { return 'overshadowed'; }
parseInt('123', 10); // the local function is called
})();
parseInt('123', 10); // the browser function is called
Is there a de jure (ECMAScript spec) or de facto (common) name for this? Overshadowing? Overloading?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…