Because window.name
is a magic property of window
object and can hold only strings (any object, including arrays, is coerced to primitive type and becomes something like "[Object Object]"
). Variables defined in global scope become properties of global object and it can cause conflicts.
You can have variable name
in any nonglobal scope. Simple workaround can be to wrap your code in immediately-invoked function expression (IIFE).
(function(){
var wrapper = document.createElement("div");
var name = document.createElement("div");
wrapper.appendChild(name); // workd
document.body.appendChild(wrapper);
}());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…