Skip to content

Instantly share code, notes, and snippets.

@duikb00t
Last active May 6, 2020 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duikb00t/4122206f65437381d05cba6316178bba to your computer and use it in GitHub Desktop.
Save duikb00t/4122206f65437381d05cba6316178bba to your computer and use it in GitHub Desktop.
Javascript
let x = { greeting: 'Hello', infotext: 'lorem ispum sit dolor amet' };
let y = Object.assign({}, x);
y.greeting = 'Worked';
console.log(x);
console.log(y);
/*
So... what does this o?
Explanation:
- First we setup an object called, x with a key greeting and infotext.
- We create a new Object and we clone it from the previous one.
- We can now change the key value easily from the cloned object.
Demo:
https://es6console.com/jvmb5r70/
*/
let c = {greeting: "hello" }
let d, q
d = c
q = d
c.greeting = "Yow";
console.log(d.greeting);
console.log(q.greeting);
/*
So... why does this work?
Explanation:
- Assignement doesn't copy, you still only have one object, just two names referring to it
Demo:
https://es6console.com/jvmasn0w/
*/
Javascript Spread operator ...
... = SPREAD operator
It's basically the deconstructor for an array
const a, b, c = ...['foo', 'bar', 'baz'];
puts 'foo' in a, 'bar' in b etc,
Example:
this.errors.push(...errors) === this.errors.push(errors[0], errors[1], etc.)
Extra information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
It's also how you can append array items to another array
const a = ['foo']; const b = ['bar', ...a];
console.log(b); // ['bar', 'foo
const a = ['foo']; const b = ['bar', ...a];
console.log(b); // ['bar', 'foo']
Works only in babel/webpack, no IE support.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment