Thursday, October 21, 2021
There are times when you will get an error when you tried to access a property in an object, sometimes you think a property exists in the object but it does not. Let's use the product object from the previous blog post as an example.
Let's say another developer works on the project and he assumes that since there's a "country" property that there should be a "city" property. If he tries to access the access the "city" property in the product object he will get an undefined, because the property does not exist. If he types the following he will get the undefined message.
product.city; or type console.log(product.city)
If the developer still persists and still tries to access property by checking it's length he will get the TypeError:
As you can see these errors are not very descriptive, so the best thing to do is to avoid them all together. The way to do that is to check to if a property exists before you access it with the example below.
var length = undefined;
if(product)
if (product.city)
length = product.city.length;
Now if console.log(length) we will get an undefined if the "city" property does not exist. It's way better than getting a TypeError exception and it's the expected result because you initialized the length variable to "undefined". Another alternative is to use the .hasOwnProperty() method or the in
operator to test if the property exists in the product object.
For the .hasOwnProperty()
The code would look something like this
if(product.hasOwnProperty("city")
length = product.city.length;
With the in operator the code would look something like this.
if("city" in product)
length = product.city.length;
var product = new Object(); product.name = "Chai"; product.category = "Tea"; product.country = "India"; product.supplier = { name: "ACME Tea Of India", location: "New Delhi" };
Let's say another developer works on the project and he assumes that since there's a "country" property that there should be a "city" property. If he tries to access the access the "city" property in the product object he will get an undefined, because the property does not exist. If he types the following he will get the undefined message.
product.city; or type console.log(product.city)
If the developer still persists and still tries to access property by checking it's length he will get the TypeError:
As you can see these errors are not very descriptive, so the best thing to do is to avoid them all together. The way to do that is to check to if a property exists before you access it with the example below.
var length = undefined;
if(product)
if (product.city)
length = product.city.length;
Now if console.log(length) we will get an undefined if the "city" property does not exist. It's way better than getting a TypeError exception and it's the expected result because you initialized the length variable to "undefined". Another alternative is to use the .hasOwnProperty() method or the in
operator to test if the property exists in the product object.
For the .hasOwnProperty()
The code would look something like this
if(product.hasOwnProperty("city")
length = product.city.length;
With the in operator the code would look something like this.
if("city" in product)
length = product.city.length;
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
Free Worldwide Shipping, Cafe Business Cards, Restaurant Business Card, Loyalty Card, Stamp Card, Pie Business Cards, Restaurant Loyalty Card - Free Shipping - Aladdin Print. Visit our website to learn more!
ReplyDeleteExcellent trancription https://www.typingservice.org/m4a-to-text-conversion-service/ provider. Adam was my initial contact and was able to answer all my questions, provide an immediate quote with full transparency on fees & expedited services. Their process is quick and efficient with excellent follow-through throughout the process. Excellent quality on final trancription & entire transaction was completed in under 24 hours. Reasonable price for outstanding service!
ReplyDelete