-
Notifications
You must be signed in to change notification settings - Fork 6
Structuring
Shane Brinkman-Davis Delamore edited this page Mar 24, 2019
·
6 revisions
See also: Destructuring and Restructuring
The opposite of destructuring, object-structuring allows you to create objects with further-reduced syntax. Inside an explicit object-literal, you can omit keys for your values and have them automatically generated for you:
a = {} # < explicit object literal block
user
post.caption
@count
info: "normal, explicit keys work here, too"
// Javascript ES6 (EcmaScript6)
a = {
user,
caption: post.caption,
count: this.count,
info: "normal, explicit keys work here, too"
};
If it is a pathed reference, the final identifier is used as the key.
- Effectively, "@" and everything before the last '.' in a dot-chain are removed when generating the key.
- This is the inverse of pathing with Destructuring.
Examples:
{} @foo # foo: @foo
{} foo.bar # bar: foo.bar
a = # < implicit
info: 123
user
post.caption
@count
foo: 123
a = [{info: 123}, user, post.caption, @count, {foo: 123}];
This provides a built-in way to 'select' props from one object, creating a new one, without listing the property names more than once:
a = {} userRecord extract name, address
b = {} myPoint extract x, y
# OR
a = {} {name, address} = userRecord
b = {} {x, y} = myPoint
let a, b,
{name, address} = userRecord,
{x, y} = myPoint;
a = {name, address};
b = {x, y};
- Home
- Get Started
- Benefits
- Highlights
- Productivity by Design
- CaffeineScript Design
- What is CaffeineScript Good For?
- Get the most out of JavaScript
- Language Comparison
- CHANGELOG
- Blocks Instead of Brackets
- Binary Line-Starts
- Everything Returns a Value
- Streamlined Modules
- Scopes and Variables
- Optional Commas
- Semantics
- Ambiguities