Eran Machiels
Eran Machiels

Reputation: 881

Element a reserved Javascript function?

I am making a javascript library where I make use of a wrapper around a HTML element (just like jQuery's $()). I wanted to call this simply Element, but later on I saw that javascript allready have an Element object: Found here...

At this moment I use my wrapper as:

var divs = new Element('div').each(function (element) {
    console.log(new Element(element));
});

Remarkable is that when I log Element in the console (console.log(Element);), it outputs the following code (and my library is not included in the webpage):

Logging Element

And the above goes the same for Node.

Therefor my question: is Element a reserved word in javascript? Or is it perfectly fine to use it as a custom wrapper since it is a "fictive" object? And actually the same question for Node, since that is is an object in javascript aswell?

Upvotes: 0

Views: 72

Answers (1)

Estus Flask
Estus Flask

Reputation: 222503

Element and Node are parts of DOM.

It is perfectly fine to use Element or Node names for variables if they are used in non-global scope (block scopes, IIFEs, ES6/CommonJS modules) and don't overwrite global ones.

Globals can still be accessed on window if needed.

(() => {

class Element { ... }
class Node { ... }

Node !== window.Node;
document instanceof window.Node === true;

})();

Upvotes: 3

Related Questions