What does it mean window.property in js?

What does it mean window.property in js?

window is a global object in JavaScript that represents the window of the browser. It has many built-in properties and methods that are available to all scripts running in the window.

When you create a property on the window object, you are creating a global variable that can be accessed from anywhere in the script. For example, if you set window.myVariable = "Hello world", you can access the value of myVariable from any other script running in the same window by referring to window.myVariable.

In general, it's not a good practice to create global variables as it can lead to naming conflicts and make the code harder to maintain. It's better to use variables with a narrower scope, such as those defined within a function or a module.

 

But I only see someone use 
var tempUrl="";
then the window get a property named as tempUrl, how does this work?

 

When you declare a variable using the var keyword in the global scope, it automatically becomes a property of the window object in the browser.

So when you declare a variable like this:

var tempUrl = "";

it creates a property on the window object called tempUrl, and you can access it using:

window.tempUrl

or simply:

tempUrl

This is because window is the global object in the browser, and any variable declared in the global scope becomes a property of the window object.

 

any variable declared in the global scope becomes a property of the window object.  Which documentation defines this?

This behavior is defined in the ECMAScript specification, which is the official specification for the JavaScript language. Specifically, the ECMAScript specification states that "The global object is the object that is the outermost object of the JavaScript object hierarchy" (source: ECMAScript Language Specification, Section 8.1).

In web browsers, the global object is the window object, which is the top-level object for JavaScript code running in the browser environment. When a variable is declared in the global scope, it becomes a property of the window object by default. This behavior is described in the W3C DOM specification, which states that "Variables declared globally become properties of the global object" (source: https://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html#GlobalBinding).

 

What's the default global object of a simple js file?

The default global object in a simple JS file is the window object, which represents the browser window or tab in which the script is running. This object is automatically created by the browser and provides a global namespace for all the JavaScript code running within the window.

In a Node.js environment, the global object is different and is called global. It provides a similar namespace for all the JavaScript code running within a Node.js process.

 

posted @ 2023-05-11 14:52  ChuckLu  阅读(6)  评论(0)    收藏  举报