Friday, October 22, 2021
When the browser window opens and navigates to a webpage with your JavaScript code. A global object is created for your JavaScript program.
If you type the following line as the first line of code in your JavaScript file
var globalObj = this;
What do you think will get? In this case you are referencing the Window object, which is JavaScript's object for the browser window. As you can see there's a whole bunch of global properties and methods already defined at the global level.
However you don't have to be at the global level to access the Window object because the Window object has a special self referencing keyword called window. For example you can get a reference to the Window object inside a function wrapped inside an anonymous function with the window keyword.
(function () {
console.log("inside function window");
console.log(window);
})();
You will still get the same result.
When working at the global scope you have to be careful because anything you defined will be added to the global object and it could have unintended consequences. For example I could create a global object pretty easily. Let's use our variable global object for this demonstration.
If you type in the following code you will be creating a global object and adding it to the Window object.
If you type the following line as the first line of code in your JavaScript file
var globalObj = this;
What do you think will get? In this case you are referencing the Window object, which is JavaScript's object for the browser window. As you can see there's a whole bunch of global properties and methods already defined at the global level.
However you don't have to be at the global level to access the Window object because the Window object has a special self referencing keyword called window. For example you can get a reference to the Window object inside a function wrapped inside an anonymous function with the window keyword.
(function () {
console.log("inside function window");
console.log(window);
})();
You will still get the same result.
When working at the global scope you have to be careful because anything you defined will be added to the global object and it could have unintended consequences. For example I could create a global object pretty easily. Let's use our variable global object for this demonstration.
If you type in the following code you will be creating a global object and adding it to the Window object.
var globalObj = this; globalObj.AGlobalObject = { name: "My Global Object", myFunct : function MyFunction(){ console.log("My Global Function!")} }; globalObj.AGlobalObject.myFunct(); console.log(globalObj);
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
There are amazing articles tutorial for java script related. My Website is Campus Management System, designed to promote paperless university administration manage
ReplyDeleteby Software. Visit: Campus Management System