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.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-05-11 How to delete specific nodes from an XElement?
2021-05-11 如何学习数据结构?
2021-05-11 3.2.1 Toy problems 8-puzzle problem
2021-05-11 CS 3600 Intro to Artificial Intelligence — Spring 2020
2021-05-11 CS 3600 – Introduction to Intelligent Systems
2020-05-11 ConcurrentDictionary测试
2020-05-11 Inside the Concurrent Collections: ConcurrentDictionary
点击右上角即可分享
微信分享提示