Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions 14 - JavaScript References VS Copying/index-START.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@

<script>
// start with strings, numbers and booleans
// let age = 100;
// let age2 = age;
// console.log(age, age2);
// age = 200;
// console.log(age, age2);

// let name = 'Wes';
// let name2 = name;
// console.log(name, name2);
// name = 'wesley';
// console.log(name, name2);
let age = 100;
let age2 = age;
console.log(age, age2);
age = 200;
console.log(age, age2);

let name ='wes';
let name2 = name;
console.log(name, name2);
name = 'wesley';
console.log(name, name2);

// Let's say we have an array
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];

// and we want to make a copy of it.
const team = players;

console.log(players, team);

// You might think we can just do something like this:
// team[3] = 'Lux';
// team[3] = 'Lux';

// however what happens when we update that array?

Expand All @@ -48,8 +48,6 @@

// or use the new ES6 Spread
const team4 = [...players];
team4[3] = 'heeee hawww';
console.log(team4);

const team5 = Array.from(players);

Expand All @@ -59,40 +57,32 @@

// with Objects
const person = {
name: 'Wes Bos',
age: 80
name: 'Wes Bos',
age: 80
};

// and think we make a copy:
// const captain = person;
// captain.number = 99;
const captain = person;
// captain.number = 99;

// how do we take a copy instead?
const cap2 = Object.assign({}, person, { number: 99, age: 12 });
console.log(cap2);
const captain2 = Object.assign({}, person, {number: 99});

// We will hopefully soon see the object ...spread
// const cap3 = {...person};
// const captain3 = {...captain};

// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.

const wes = {
name: 'Wes',
age: 100,
social: {
twitter: '@wesbos',
facebook: 'wesbos.developer'
}
name: 'Wes',
age: 100,
social: {
twitter: '',
facebook: ''
}
};

console.clear();
console.log(wes);

const dev = Object.assign({}, wes);

const dev2 = JSON.parse(JSON.stringify(wes));


</script>

</body>
Expand Down