Thursday, November 4, 2021
In the previous post we visited how to create an object with an object literal. In this post we are going to create the same object using the new keyword. When creating objects using the new keyword it is required that the it is followed by a function call. The purpose of this function call is the needs a way to call to the constructor of the new object. A constructor is used to create or initialize the created object.
Here is the code to rewrite our literal object using the new keyword:
var product = new Object();
product.name = "Chai";
product.category= "Tea";
product.country= "India";
product.supplier= {
name: "ACME Tea Of India",
location: "New Delhi"
};
product.related = new Array("Earl Grey", "Green Tea", "Dark Tea", "White Tea");
product.display = function () {
console.log(this);
};
Notice the object is the same as the object we created using the literal syntax but with the new keyword. Also we've created a custom object with the line
var product = new Object();
But we've also created a native object with the line:
product.related = new Array("Earl Grey", "Green Tea", "Dark Tea", "White Tea");
So the new keyword can be used to create both a custom object and a native object.
Here is the code to rewrite our literal object using the new keyword:
var product = new Object();
product.name = "Chai";
product.category= "Tea";
product.country= "India";
product.supplier= {
name: "ACME Tea Of India",
location: "New Delhi"
};
product.related = new Array("Earl Grey", "Green Tea", "Dark Tea", "White Tea");
product.display = function () {
console.log(this);
};
Notice the object is the same as the object we created using the literal syntax but with the new keyword. Also we've created a custom object with the line
var product = new Object();
But we've also created a native object with the line:
product.related = new Array("Earl Grey", "Green Tea", "Dark Tea", "White Tea");
So the new keyword can be used to create both a custom object and a native object.
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
karabük
ReplyDeletesiirt
niğde
düzce
karaman
E5TKW