JavaScript · Chapter 14 of 55
Object Properties & Methods
Beyond simple key-value data, objects can have methods — functions attached as properties — that operate on the object's own data using the `this` keyword.
You can add, modify, or delete properties on an object at any time after creation, since JavaScript objects are dynamic.
Adding and deleting
`obj.newProp = value;` adds a property. `delete obj.prop;` removes it entirely.
this in methods
Inside a regular method, `this` refers to the object the method was called on, letting methods access sibling properties.
Example 1 (javascript)
let user = { name: "Lee" };
user.age = 30;
delete user.name;
console.log(user);Output
{ age: 30 }Properties can be added and removed dynamically.
Example 2 (javascript)
let rect = {
width: 4, height: 5,
area() { return this.width * this.height; }
};
console.log(rect.area());Output
20this.width and this.height refer back to the object's own properties.
Key points
- Properties can be added or removed after object creation.
- `delete obj.prop` removes a property.
- `this` inside a method refers to the calling object.
- Objects can also have computed and shorthand properties.
💡 Note: Arrow functions do not bind their own `this`, so avoid them for object methods that need `this`.
