This file contains hidden or 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
//We want to set the 'cookies' value to each property | |
function reviver(key, value) { | |
if(key === '') return value; // In this property, our entire object has been wrapped up. So we can't change it, because the reviver passes through the properties from the most nested to this property. And if we overwrote it, we would lose references indicating where all the properties of our source are. | |
else return 'cookies'; // for the others, we set the value 'cookies' | |
}; | |
const obj = { | |
one: 1, // is present in the text parameter of the parse method | |
two: 2, // is present in the text parameter of the parse method | |
func: function() {}, // stringify method will omit this property as JSON doesn't support functions |
This file contains hidden or 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
//Our goal is simple, we want to set every property value on 'cookies' | |
function replacer(key, value) { | |
if(key === "") return value; // the "" property is the one where the whole object passed as the source to the stringify method is wrapped up. Thus we cannot change this property otherwise we lose the reference to the place where the whole source object is. In other words, there would be no more the source object | |
else return 'cookies'; // When we got to our source object we set each of its properties on 'cookies' since that was our goal. | |
}; | |
const obj = { | |
one: 1, // set on cookies | |
two: 2, // set on cookies | |
func: function() {}, // set on cookies |
This file contains hidden or 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
const obj = { | |
one: 1, // spiecified in the replacer array, therefore, present in the resulting JSON string | |
two: 2, // didn't specify in replacer array, therefore, omitted | |
func: function() {}, // specified but JSON doesn't support functions->look at the considerations above (simply omitted totaly when in object) | |
symb: Symbol(), // specified but the same as with functions | |
undef: undefined, //specified but the same as with functions and symbols | |
nothing: null, // specified and supported, therefore, present in the resulting JSON string | |
array: [1, 'fruits', function() {}], // as the property with 'array' key is specified but the third element (function) will be omitted since JSON doesn't support functions. Look above to see with what such a function would be replaced in the resulting JSON string (null). | |
food: { | |
vegetables: 'vegetables', //ommited, therefore, doesn't present in the resulting JSON even though the string type is supported. In other words it is filtered out //fro |
This file contains hidden or 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
const object = { | |
func: function() { return 'Cookies'; }, | |
symb: Symbol('Cookies'), | |
und: undefined | |
}; | |
const stringify = JSON.stringify(object); | |
console.log(stringify); |
This file contains hidden or 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
const array = [ | |
function() {}, | |
Symbol(), | |
undefined | |
]; | |
const stringify = JSON.stringify(array); | |
console.log(stringify); |
This file contains hidden or 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
const stringify = [JSON.stringify(function() { return 'Cookies'; }), JSON.stringify(Symbol("Cookies")), JSON.stringify(undefined)]; | |
console.log(stringify); | |
/* | |
(3) [undefined, undefined, undefined] | |
0: undefined | |
1: undefined | |
2: undefined | |
length: 3 |
This file contains hidden or 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
const o1 = { | |
one: 1, | |
o2: { | |
two: 2 | |
} | |
}; | |
Object.defineProperty(o1, 'p', { | |
value: 'p' | |
}); |
This file contains hidden or 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
function deepCopy(copyTo, copiedFrom) { | |
let property, | |
toString = Object.prototype.toString; | |
copyTo = copyTo || {}; | |
for(property in copiedFrom) { | |
if(copiedFrom.hasOwnProperty(property)) { | |
if(typeof copiedFrom[property] === 'object') { | |
copyTo[property] = toString.call(copiedFrom[property]) === '[object Array]' ? [] : {}; | |
deepCopy(copyTo[property], copiedFrom[property]); | |
} |
This file contains hidden or 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
function array(note, one, two, three, ...rest) { | |
console.log(note, one, two, three, '\n', rest); | |
} | |
array("Example of the rest parameter in action", 1, 2, 3, 4, 5, 'etc.'); | |
/* | |
Example of the rest parameter in action 1 2 3 [4, 5, "etc."] | |
*/ |
This file contains hidden or 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
function array(note, one, two, three) { | |
console.log(note, one, two, three, '\n', arguments); | |
} | |
array("when less arguments than parameters", ...[1, 2]); | |
array("when equal arguments to parameters", ...[1, 2, 3]); | |
array("when more arguments than parameters", ...[1, 2, 3, 4]); | |
/* | |
when less arguments than parameters 1 2 undefined ["when less arguments than parameters", 1, 2, callee: ƒ, Symbol(Symbol.iterator): ƒ] |
NewerOlder