Thursday, October 28, 2021
In JavaScript there are two ways to access an object, first by dot notation like this
product.name
or with array notation
product["name"]
They both get the job done. However with the array syntax [""] you access the object as if it's an array but instead of accessing it by numbers or index you access it by the property names. That's because JavaScript objects are associative arrays. They look like other objects in other languages such as C, C++, and Java. However they behave very differently.
In other languages objects are defined in advance, their properties are methods are fixed. To add a property or method to the object you have to change the class the object instance is created from.
JavaScript on the other hand is a loosely typed language. What does that mean? That means the JavaScript program can create properties in any object. In EMCAScript 5 and above you can set the object's writable property to false, thus preventing anyone who else from modifying your object. But that's beyond the scope of this blog. Sorry I always wanted to say that!...lol
So what is the advantage of an array notation? Let's say you have a scenario where you want to store the number of cookies a kid in kindergarten has. With the array notation it's very easy to store that information.
Let's demonstrate this feature with code:
var kindergarten = new Object();
keepTrackOfCookies(kindergarten, "Mikey", 15);
keepTrackOfCookies(kindergarten, "Christine", 1);
keepTrackOfCookies(kindergarten, "David", 2);
function keepTrackOfCookies(kindergarten, kid, cookies) {
kindergarten[kid] = cookies;
}
console.log("kindergarten object: ");
console.log(kindergarten);
Here is the output:
As you can see beside the fact that "Mikey" was very hungry, it's very easy to keep track of how many cookies each kids have in the kindergarten class with the array notation.
product.name
or with array notation
product["name"]
They both get the job done. However with the array syntax [""] you access the object as if it's an array but instead of accessing it by numbers or index you access it by the property names. That's because JavaScript objects are associative arrays. They look like other objects in other languages such as C, C++, and Java. However they behave very differently.
In other languages objects are defined in advance, their properties are methods are fixed. To add a property or method to the object you have to change the class the object instance is created from.
JavaScript on the other hand is a loosely typed language. What does that mean? That means the JavaScript program can create properties in any object. In EMCAScript 5 and above you can set the object's writable property to false, thus preventing anyone who else from modifying your object. But that's beyond the scope of this blog. Sorry I always wanted to say that!...lol
So what is the advantage of an array notation? Let's say you have a scenario where you want to store the number of cookies a kid in kindergarten has. With the array notation it's very easy to store that information.
Let's demonstrate this feature with code:
var kindergarten = new Object();
keepTrackOfCookies(kindergarten, "Mikey", 15);
keepTrackOfCookies(kindergarten, "Christine", 1);
keepTrackOfCookies(kindergarten, "David", 2);
function keepTrackOfCookies(kindergarten, kid, cookies) {
kindergarten[kid] = cookies;
}
console.log("kindergarten object: ");
console.log(kindergarten);
Here is the output:
As you can see beside the fact that "Mikey" was very hungry, it's very easy to keep track of how many cookies each kids have in the kindergarten class with the array notation.
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
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
DevOps
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
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
Software Development
Unix
View
Web Forms
WildFly
XML
cshtml
githu
A firmware file is used to upgrade or install your operating system again on your device. This file is unique for every phone. If you are facing any issues like software issues and Boot loop issues, you can download this file to fix all the issues.Infocus m430 how to flash
ReplyDeleteLoved the content! You have covered all aspects of the subject in this article, Thanks a lot.
ReplyDeleteArtificial Intelligence Training in Hyderabad
Artificial Intelligence Course in Hyderabad
We establish strong internal security culture to provide convenience so that your devices can identify and respond to threats.
ReplyDelete