Loading Please Wait...
You have already learned that JavaScript variables are containers for data values. Objects are variables too. But objects can contain many values.
The values are written as name:value pairs (name and value separated by a colon).
It is a common practice to declare objects with the const keyword.
const user = {name:"John Doe", email:"john@gmail.com"};
You define (and create) a JavaScript object with an object literal.
const user1 = {name:"John Doe", email:"john@gmail.com"};
const user2 = {
name:"John Doe",
email:"john@gmail.com"
};
The name:values pairs in JavaScript objects are called properties.
You can access object properties in two ways:
objectName.propertyName
// or
objectName["propertyName"]
user.name // John Doe
user["email"] // john@gmail.com
Objects can also have methods (or functions).
Methods are actions that can be performed on objects.
Methods are stored in properties as function definitions.
A method is a function stored as a property.
const user = {
firstName:"John",
lastName:"Doe",
email:"john@gmail.com",
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
// Accessing object methods
user.fullName(); // John Doe
user.fullName; // function definition itself
In JavaScript, the this keyword refers to an object.
Which object depends on how this is being invoked (used or called).
The this keyword refers to different objects depending on how it is used
Alone, this refers to the global object.
In a function, this refers to the global object.
In an event, this refers to the element that received the event.
Methods like call(), apply(), and bind() can refer this to any object.
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved