Skip to content

Instantly share code, notes, and snippets.

@wo17ek
wo17ek / Demonstrate the behavior when the reviver is defined.js
Last active March 20, 2020 11:36
Demonstrate the behavior when the reviver is defined.
//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
@wo17ek
wo17ek / Demonstrate the behavior when the replacer is a function.js
Last active March 20, 2020 11:36
Demonstrate the behavior when the replacer is a function.
//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
@wo17ek
wo17ek / Demonstrate the behavior when the replacer is an array.js
Last active March 20, 2020 11:37
Demonstrate the behavior when the replacer is an array.
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
@wo17ek
wo17ek / The result when function, symbol, or undefined is a member of an object.js
Last active March 20, 2020 11:38
The result when function, symbol, or undefined is a member of an object.
const object = {
func: function() { return 'Cookies'; },
symb: Symbol('Cookies'),
und: undefined
};
const stringify = JSON.stringify(object);
console.log(stringify);
@wo17ek
wo17ek / The result when function, symbol, or undefined is a member of an array.js
Last active March 20, 2020 11:38
The result when function, symbol, or undefined is a member of an array.
const array = [
function() {},
Symbol(),
undefined
];
const stringify = JSON.stringify(array);
console.log(stringify);
@wo17ek
wo17ek / The result when directly passing function, symbol or undefined.js
Last active March 20, 2020 11:38
The result when directly passing function, symbol or undefined.
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
@wo17ek
wo17ek / The presentation of skipping all properties whose enumerable flag is set on false.js
Last active March 20, 2020 11:39
The presentation of skipping all properties whose enumerable flag is set on false.
const o1 = {
one: 1,
o2: {
two: 2
}
};
Object.defineProperty(o1, 'p', {
value: 'p'
});
@wo17ek
wo17ek / A circular reference occurrence resulting with exceed call stack.js
Last active March 20, 2020 11:39
A circular reference occurrence resulting with exceed call stack.
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]);
}
@wo17ek
wo17ek / The example of applying the rest parameter to function.js
Last active March 20, 2020 11:40
The example of applying the rest parameter to function.
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."]
*/
@wo17ek
wo17ek / Spreading arrays to the function when less, equal, and more arguments than parameters.js
Last active March 20, 2020 11:40
Spreading arrays to the function when less, equal, and more arguments than parameters.
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): ƒ]