Object:
*Objects contains information or data.
*Objects are variables that can Store both values and functions.
*JavaScript objects are collections of properties
Properties can be changed, added and deleted.
Example:
const car = {
type: "Fiat",
model: "500",
color: "white"
};
type, model, and color are properties
"Fiat", "500", and "white" are property values
*Methods are actions that can be performed on objects.
Methods are functions stored as property values.
const person = {
firstName: "John",
lastName : "Doe",
age : 50,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
Output: John Doe
Property Property Value
firstName John
lastName Doe
age 50
fullName function() {return this.firstName + " " + this.lastName;}
In an object method, this refers to the object.
In the example above, this refers to the person object.
Top comments (4)
I found the explanation of objects in JavaScript to be quite clear, especially the distinction between properties and methods. I've worked with objects in JavaScript for a while now, and I can attest to the fact that understanding how methods are functions stored as property values is crucial for effective object-oriented programming. One thing I've learned from my experience is that it's essential to be mindful of the context in which
thisis used, as it can sometimes lead to unexpected behavior if not properly understood. I'm curious, have you encountered any common pitfalls or gotchas when working with objects and methods in JavaScript, and if so, how do you avoid them?I haven't worked with object in javascript. So I'm not faced this problem. If I have that issue , l will ping on you
Thanks 👍
Some comments may only be visible to logged-in visitors. Sign in to view all comments.