Wednesday, March 18, 2015
JavaScript objects acts differently than objects in the object-oriented programming world. A JavaScript object is mutable meaning it can be changed and is dynamic.
Here is how you would create a JavaScript object:
Or you can create a JavaScript object in a literal format
The mutabiity of a JavaScript object
Unlike traditional programming languages such as C# or Java, when you work with a JavaScript object you can add a property or method to an object dynamically. For instance let's say you've already declared the Employee object and you wanted to add the property "Title" to object. How would you do this? Well in a C# or Java you would have to go back to the class definition and add the property in the class definition file. But in JavaScript you can add the property dynamically in your code as you need it. Like the example below.
Accessing Object Methods and Functions
You access the object methods and functions with the . notation. Meaning if you want to access the value for the employee's first name you would access it like this Employee.FirstName. Like the example below:
Setting Object Properties And Methods
You can set object properties and methods in two ways. Either with the object name followed by a . and then the property/method name, or the object name with the property/method name enclosed within a [ ]
Let's say your object is getting too big and you want to eliminate some of the properties and methods that you are not using. Here is how you can do it.
Here is how you would create a JavaScript object:
var Employee = new Object(); Employee.ID = 1; Employee.FirstName = "Jeff"; Employee.LastName = "Jordan";You create a new object the new Object() constructor
Or you can create a JavaScript object in a literal format
var Employee = { ID: 1, FirstName: "Jeff", LastName: "Jordan" };You can also include a function as a method in your object like the example below
var Employee = { ID: 1, FirstName: "Jeff", LastName: "Jordan", displayEmployee: function() { document.writeln("Employee ID: " + this.ID); document.writeln("First Name: " + this.FirstName); document.writeln("Last Name: " + this.LastName); };
The mutabiity of a JavaScript object
Unlike traditional programming languages such as C# or Java, when you work with a JavaScript object you can add a property or method to an object dynamically. For instance let's say you've already declared the Employee object and you wanted to add the property "Title" to object. How would you do this? Well in a C# or Java you would have to go back to the class definition and add the property in the class definition file. But in JavaScript you can add the property dynamically in your code as you need it. Like the example below.
var Employee = { ID: 1, FirstName: "Jeff", LastName: "Jordan", displayEmployee: function() { document.writeln("Employee ID: " + this.ID); document.writeln("First Name: " + this.FirstName); document.writeln("Last Name: " + this.LastName); }; Employee.Title = "CEO";
Accessing Object Methods and Functions
You access the object methods and functions with the . notation. Meaning if you want to access the value for the employee's first name you would access it like this Employee.FirstName. Like the example below:
document.writeln("Employee ID: " + Employee.ID); document.writeln("First Name: " + Employee.FirstName); document.writeln("Last Name: " + Employee.LastName);
Setting Object Properties And Methods
You can set object properties and methods in two ways. Either with the object name followed by a . and then the property/method name, or the object name with the property/method name enclosed within a [ ]
Employee.FirstName = "Jackie"; Employee["LastName"] = "Johnson";Deleting Object Methods Or Functions
Let's say your object is getting too big and you want to eliminate some of the properties and methods that you are not using. Here is how you can do it.
delete Employee.displayEmployee; delete Employee["Title"];
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
Aivivu - đại lý chuyên vé máy bay trong nước và quốc tế
ReplyDeletevé máy bay đi Mỹ hạng thương gia
ve may bay ve vietnam tu my
vé máy bay nhật về việt nam
vé máy bay khứ hồi từ đức về việt nam
lịch bay từ canada về việt nam
Chuyến bay từ Hàn Quốc về Việt Nam
chuyen bay chuyen gia ve viet nam