Last active
October 19, 2018 15:14
-
-
Save Clickys/f8e519eb5a1036a1a873aac06c09977f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// VIDEO REMAINING INTERNAL HELPER METHODS | |
// QUESTIONS AND OBSERVATIONS | |
//QUESTION 1: why object[key] == null and not object[key] === undefined ? Why its compare it with null instead of undefined. Because if an object property is missing obj['prop'] it will return undefined not null. | |
// QUESTION 2: object[key] = defs[key]; if defaultObject has an array as property with couple of values instead of primitive values then it will copy to the new return object that array by reference and if you change a value to one it will change to all. | |
var defaultCar = { | |
color: ['grey', 'red'], | |
wheels: 4, | |
tires: 'standard', | |
} | |
var andreasCar = { | |
tires: 'expensive' | |
} | |
function defaults(object, defs) { | |
var key; | |
object = object || {}; | |
defs = defs || {}; | |
for (key in defs) { | |
if (defs.hasOwnProperty(key)) { | |
if (object[key] == null) { // QUESTION 1 | |
object[key] = defs[key]; | |
} | |
} | |
} | |
return object; | |
} | |
// QUESTION 1 | |
console.log(andreasCar['wheels'] === undefined); | |
// QUESTION 2 | |
let newObject = defaults(andreasCar, defaultCar); | |
newObject.color[0] = 'changed value'; | |
console.log(`New object colors: ${newObject.color[0]} , andreasCar colors: ${andreasCar.color[0]} , defaultCar color: ${defaultCar.color[0]}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment