Object Spread Syntax
12345678910111213const baseUser = { name: "Alice", age: 30, role: "user" }; const adminUser = { ...baseUser, role: "admin", permissions: ["read", "write", "delete"] }; console.log(JSON.stringify(adminUser));
The object spread syntax (...) offers a concise way to copy and extend objects. In the example above, adminUser is created by copying all properties from baseUser and then overriding the role property while adding a new permissions property. This approach is more readable and less error-prone than Object.assign. With Object.assign, you would write:
const adminUser = Object.assign({}, baseUser, {
role: "admin",
permissions: ["read", "write", "delete"]
});
Both patterns perform a shallow copy, but the spread syntax is more succinct and easier to use, especially when combining properties or extending objects with new values.
When extending objects, always use shallow copy techniques like the spread syntax (...) or Object.assign to avoid modifying the original object. This helps prevent bugs and keeps your data predictable. Avoid copying objects with properties that are themselves objects or arrays if you need deep copies, as both the spread operator and Object.assign only copy references for nested objects. To avoid prototype pollution, never spread or assign objects from untrusted sources directly into your application's main objects. Always validate and sanitize input before extending objects, and prefer creating new objects over mutating existing ones.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 7.69
Object Spread Syntax
Stryg for at vise menuen
12345678910111213const baseUser = { name: "Alice", age: 30, role: "user" }; const adminUser = { ...baseUser, role: "admin", permissions: ["read", "write", "delete"] }; console.log(JSON.stringify(adminUser));
The object spread syntax (...) offers a concise way to copy and extend objects. In the example above, adminUser is created by copying all properties from baseUser and then overriding the role property while adding a new permissions property. This approach is more readable and less error-prone than Object.assign. With Object.assign, you would write:
const adminUser = Object.assign({}, baseUser, {
role: "admin",
permissions: ["read", "write", "delete"]
});
Both patterns perform a shallow copy, but the spread syntax is more succinct and easier to use, especially when combining properties or extending objects with new values.
When extending objects, always use shallow copy techniques like the spread syntax (...) or Object.assign to avoid modifying the original object. This helps prevent bugs and keeps your data predictable. Avoid copying objects with properties that are themselves objects or arrays if you need deep copies, as both the spread operator and Object.assign only copy references for nested objects. To avoid prototype pollution, never spread or assign objects from untrusted sources directly into your application's main objects. Always validate and sanitize input before extending objects, and prefer creating new objects over mutating existing ones.
Tak for dine kommentarer!