I was wondering if I can add to Javascript's document
object. Like:
var document = {
name: "My Name"
}
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With