In JavaScript, an object is a collection of key-value pairs, similar to a dictionary or hash table in other programming languages.
Objects can be used to represent real-world objects, such as a person, a car, or a bank account.
Properties are the values associated with a JavaScript object and methods are actions that can be performed on objects.
Objects can also be nested, meaning an object can have properties that are themselves objects.
Objects can be created using object literals or constructor functions.
Here's an example of an object in JavaScript using an object literal:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
This object, named person, has five properties: firstName, lastName, age, isStudent and a method fullName. The properties have the values "John", "Doe", 30, false respectively.
The method fullName returns a string that is the concatenation of the firstName and lastName properties.
You can access the properties and methods of an object using the dot . notation or the bracket [ ] notation:
console.log(person.firstName); // "John"
console.log(person["lastName"]); // "Doe"
console.log(person.fullName()); // "John Doe"
You can also add, remove or update the properties of an object:
person.isStudent = true;
console.log(person.isStudent); // true
delete person.age;
console.log(person.age); // undefined
This is just a basic example, objects in JavaScript can be used in much more complex ways and also can be used with many built-in objects.
Object Properties
In JavaScript, an object property is a value associated with a key. Properties are used to store data in an object and can be accessed or modified using the dot notation or the bracket notation.
Here's an example of an object with properties:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false
};
This object, named person, has four properties: firstName, lastName, age, and isStudent. The properties have the values "John", "Doe", 30, and false respectively.
You can access the properties of an object using the dot notation or the bracket notation:
console.log(person.firstName); // "John"
console.log(person["lastName"]); // "Doe"
You can also add, remove, or update the properties of an object:
person.isStudent = true;
console.log(person.isStudent); // true
delete person.age;
console.log(person.age); // undefined
Properties can be also be defined with the Object.defineProperty and Object.defineProperties methods.
These methods allow you to define properties with additional characteristics such as setting property as read-only or non-enumerable.
It's important to note that property keys are strings, even if you use the dot notation or the bracket notation to access them.
Object Methods
In JavaScript, an object method is a function that is associated with an object.
Methods are used to perform actions on an object and can be accessed or invoked using the dot notation or the bracket notation.
Here's an example of an object with a method:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
isStudent: false,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
This object, named person, has a method named fullName. The method is a function that concatenates the firstName and lastName properties and returns the full name as a string.
You can invoke the method of an object using the dot notation or the bracket notation:
console.log(person.fullName()); // "John Doe"
console.log(person["fullName"]()); // "John Doe"
It's important to note that when you use the dot notation to access the method, you need to include the parentheses () to invoke the function.
Without the parentheses, you are just accessing the function object, not invoking it.
In addition to the object's own methods, JavaScript objects also inherit methods from their prototype.
For example, all objects inherit the toString() method from the Object.prototype.
You can also add methods to an object using the Object.defineProperty() and Object.defineProperties() methods, which allows you to define properties with additional characteristics such as setting the property as read-only or non-enumerable.
What is "this" in JavaScript
In JavaScript, this keyword is a reference to the current object. The value of this depends on the context in which it is used.
In the global context, this refers to the global object, which is window in the browser and global in Node.js.
When a function is invoked as a method of an object, this refers to the object that the method is associated with. For example:
let person = {
firstName: "John",
lastName: "Doe",
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
console.log(person.fullName()); // "John Doe"
Here, when the fullName method is invoked, this refers to the person object and the method is able to access the firstName and lastName properties of the object.
In arrow functions, this is lexically scoped, meaning it refers to the value of this in the enclosing scope.
In constructor functions, this refers to the object that is being created by the constructor:
function Person(first, last) {
this.firstName = first;
this.lastName = last;
}
let john = new Person("John", "Doe");
console.log(john.firstName); // "John"
JavaScript also provides a call() and apply() method that allow you to explicitly set the value of this when invoking a function.
It's important to be careful when using this in JavaScript, as the value of this can change depending on the context in which it is used.