Thursday, October 14, 2021
Most people don't realize JavaScript has object property setters and getters or accessor methods. When an object has a setter only it is a write only property, when it has a getter only method that it is a read only property. If a property has both then it is a read/write property. A perfect example is if you are working with a private property in JavaScript which has the $ prefix in front of it.
Let's say you have an object with a private property $n like the example below:
The code above only has a getter name that exposes the private property $n when you console.log you should get value of the $n like the code snippet below
As you can see the output is as expected, it prints out the value of $n the private property because we have a getter method name() things get interesting when we try to assign a value to name. Its a bit confusing because we access the name method with a dot notation, the same as how we would access a property.
So if type the code below:
game.name = "Not Awesome Game!";
console.log(game.name);
What do you think the output will be?
"Awesome Game!" or "Not Awesome Game!"
If you guess "Awesome Game!" then you are correct because the $n property only has a getter method and its read only. Even though we assign a new value to the property there's no setter method therefore, the new value is not assigned to the $n property.
However, if we change the object definition to the code below with the setter method the $n property would be changed to "Not Awesome Game!"
Now if type
game.name = "Not Awesome Game!";
console.log(game.name);
We will get a different result:
The complete code for this post looks like the following:
Let's say you have an object with a private property $n like the example below:
var game = { $n: "Awesome Game!", get name() { return this.$n} };
The code above only has a getter name that exposes the private property $n when you console.log you should get value of the $n like the code snippet below
console.log(game.name);
As you can see the output is as expected, it prints out the value of $n the private property because we have a getter method name() things get interesting when we try to assign a value to name. Its a bit confusing because we access the name method with a dot notation, the same as how we would access a property.
So if type the code below:
game.name = "Not Awesome Game!";
console.log(game.name);
What do you think the output will be?
"Awesome Game!" or "Not Awesome Game!"
If you guess "Awesome Game!" then you are correct because the $n property only has a getter method and its read only. Even though we assign a new value to the property there's no setter method therefore, the new value is not assigned to the $n property.
However, if we change the object definition to the code below with the setter method the $n property would be changed to "Not Awesome Game!"
var game = { $n: "Awesome Game!", get name() { return this.$n }, set name(newName) { this.$n = newName; } };
Now if type
game.name = "Not Awesome Game!";
console.log(game.name);
We will get a different result:
The complete code for this post looks like the following:
var game = { $n: "Awesome Game!", get name() { return this.$n }, set name(newName) { this.$n = newName; } }; console.log(game.name); game.name = "Not Awesome Game!"; console.log(game.name);
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
I have found many games but here GTA 5 apk free download is available.
ReplyDeleteGet unlimited money, coins & diamonds for free in Mango Live Mod APK. Enjoy live streaming, video chats and calls with your friends worldwide.
ReplyDeletei have many font of apps but here ZEWhatsApp!
ReplyDeleteis avaialable .
Now go for tekken 6 apk download and play your favorite childhood game on your mobile.
ReplyDeleteAlthough there are numerous WhatsApp available, Yo WhatsApp Download is here.
ReplyDelete