Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Javascript's "document" object be used as a variable?

I was wondering if I can add to Javascript's document object. Like:

var document = {
    name: "My Name"   
}
  • Is that legal in JavaScript?
  • If it is, is it considered "good practice", or should I avoid it?
  • If I define it as a variable, is it then considered as a unique object, and not a JS DOM object?

I am plenty new to JavaScript and any help would be much appreciated.

Edit:

After some conversation in the comments and a little more thought on it, I really want to know if I can manipulate the document variable and still use document for normal use.

like image 480
ModernDesigner Avatar asked Jan 17 '13 00:01

ModernDesigner


People also ask

What is JavaScript document variable?

Document Variables provide a mechanism to store information in the document in a key-value format. DocumentVariableField is a Field element used to access and display the value, which corresponds to the given field-argument. The argument is the name of the variable.

What is the use of document object?

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects; that way, programming languages can interact with the page.

Which of the following is not a valid JavaScript variable name?

JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 123test is an invalid variable name but _123test is a valid one. JavaScript variable names are case-sensitive.

How do you save the content of an HTML element to a variable?

Answer: Use the concatenation operator (+) The simple and safest way to use the concatenation operator ( + ) to assign or store a bock of HTML code in a JavaScript variable.


2 Answers

  • Yes you can do that
  • No you shouldn't do that

document is just another property of the global / window object. If you declare it as formal parameter or variable within a scope, the lookup process will match that name first in that scope you declared it, so it kinda overlaps it.

Regardless, you don't want to do it, why would you ? Its so confusing for you and anybody else who is looking at your code. Its very bad practice.

Actually, that is the reason why most "advanced" javascript snippets / libraries begin with a closured function scope like

(function( window, document, undefined ) {
    // window will always reference the "window" object that got originally passed in
    // document will always reference the "document" object that got originally passed in
    // undefined will always reference the "undefined value" that got originally passed in
}( window, window.document ));

..just to avoid, the such called asshole effect. If such a closure is openend at the very top of a file, it makes sure that you reference the original objects within, just in case some genius had the great idea to overwrite/overlap them.

Conclusion: You are of course free to choose the name of your variables at will, but you really shouldn't use the name of such prominent names like window, document, undefined et cetera for obvious reasons.

like image 62
jAndy Avatar answered Oct 05 '22 05:10

jAndy


What you say you're doing - adding to the document object - is not what your code is actually doing.

What you are doing is redifining document. If you want to add to it then use the following:

document.name = "My Name"

This way you can still use document. However, it's still generally bad practice - you can just use a different object instead of document:

myObject = {name:"My Name"};

Then myObject is still global. Modifying the document object can have unfortunate side-effects.

like image 42
Jeff Avatar answered Oct 05 '22 03:10

Jeff