JavaScript Object Methods
Object methods are functions stored as object properties. JavaScript also provides powerful static methods on the Object constructor — like Object.keys(), Object.values(), Object.entries(), Object.assign(), Object.freeze(), and Object.seal() — for working with objects.
Defining Methods
A method is a function that belongs to an object. You can define methods using regular function syntax or the ES6 method shorthand. Inside a method, 'this' refers to the object the method belongs to.
let person = {
name: "Alice",
age: 30,
// Method shorthand (ES6)
greet() {
return `Hi, I'm ${this.name}`;
},
// Traditional method syntax
getAge: function() {
return this.age;
}
};
console.log(person.greet());
console.log(person.getAge());
// Adding a method later
person.introduce = function() {
return `${this.name}, age ${this.age}`;
};
console.log(person.introduce());Object.keys(), Object.values(), Object.entries()
These static methods return arrays of an object's own enumerable property names, values, or key-value pairs respectively. They are essential for iterating over objects.
let car = {
brand: "Toyota",
model: "Camry",
year: 2024,
color: "blue"
};
// Object.keys() — array of property names
console.log(Object.keys(car));
// Object.values() — array of values
console.log(Object.values(car));
// Object.entries() — array of [key, value] pairs
console.log(Object.entries(car));
// Useful for iteration
Object.entries(car).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});Object.assign()
Object.assign() copies all enumerable own properties from one or more source objects to a target object. It returns the modified target. This is commonly used for merging objects or creating shallow copies.
// Merge objects
let defaults = { theme: "light", lang: "en", debug: false };
let userPrefs = { theme: "dark", fontSize: 16 };
let settings = Object.assign({}, defaults, userPrefs);
console.log(settings);
// Later sources overwrite earlier ones
let a = { x: 1, y: 2 };
let b = { y: 3, z: 4 };
let merged = Object.assign({}, a, b);
console.log(merged);
// Shallow copy
let original = { name: "Alice", scores: [90, 85] };
let copy = Object.assign({}, original);
copy.name = "Bob";
console.log(original.name);
// Warning: nested objects are still shared (shallow!)
copy.scores.push(100);
console.log(original.scores);Object.freeze()
Object.freeze() makes an object immutable — you cannot add, delete, or modify its properties. Attempts to change frozen objects silently fail (or throw in strict mode). The freeze is shallow: nested objects are NOT frozen.
let config = {
apiUrl: "https://api.example.com",
timeout: 5000,
retries: 3
};
Object.freeze(config);
// Modifications silently fail
config.timeout = 10000;
config.newProp = "test";
delete config.retries;
console.log(config.timeout);
console.log(config.newProp);
console.log(config.retries);
// Check if frozen
console.log(Object.isFrozen(config));
// Shallow freeze — nested objects are NOT frozen
let deep = { nested: { value: 1 } };
Object.freeze(deep);
deep.nested.value = 999;
console.log(deep.nested.value);Object.seal()
Object.seal() prevents adding or deleting properties, but still allows modifying existing property values. It is less restrictive than freeze() — you can change values but cannot change the structure.
let user = {
name: "Alice",
role: "user"
};
Object.seal(user);
// Can modify existing properties
user.role = "admin";
console.log(user.role);
// Cannot add new properties
user.email = "alice@test.com";
console.log(user.email);
// Cannot delete properties
delete user.name;
console.log(user.name);
console.log(Object.isSealed(user));| Method | Description |
|---|---|
| Object.keys(obj) | Returns array of own property names |
| Object.values(obj) | Returns array of own property values |
| Object.entries(obj) | Returns array of [key, value] pairs |
| Object.assign(target, ...sources) | Copy properties into target |
| Object.freeze(obj) | Make object fully immutable (shallow) |
| Object.seal(obj) | Prevent add/delete but allow modify |
| Object.isFrozen(obj) | Check if object is frozen |
| Object.isSealed(obj) | Check if object is sealed |