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
var newObj = JSON.parse( JSON.stringify( someObj ) ); |
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
/** | |
* Maybe good enough for interview Simpliest Version of deepClone. | |
* Problems: | |
* 1. the type check of object is not strict | |
* 2. did not cover array | |
* 3. did not cover null | |
* @param {Object} sourceObj | |
* @returns | |
*/ | |
var deepCloneSimple = (sourceObj) => { |
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
/** | |
* You should be good with this solution | |
* More comprehensive deepclone with JS. Cover null and Array. | |
* Problems: | |
* 1. did not solve the circular dependency | |
* @param {Object} sourceObj | |
* @returns | |
*/ | |
var deepCloneBetter = (sourceObj) => { | |
// if we are not dealing with complex data structure |
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
/** | |
* Ultimate version solving circular dependency with weakMap | |
* Problems: | |
* 1. Symbols not covered | |
* | |
* @param {Object} sourceObj | |
* @param {WeakMap} [hash=new WeakMap()] | |
* @returns | |
*/ | |
var deepCloneUltimate = (sourceObj, hash = new WeakMap()) => { |
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
const objectContructor = (...args) => { | |
//get the constructor function | |
let constructor = [].shift.call(args); | |
/** | |
* create a new object and link the prototype to the function prototype | |
* equals to var obj = new Object(), obj.__proto__ = Constructor.prototype | |
*/ | |
let obj = Object.create(constructor.prototype); | |
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
const objectContructorUltimate = (...args) => { | |
let constructor = [].shift.call(args); | |
let obj = Object.create(constructor.prototype); | |
let result = constructor.apply(obj, args); | |
// ensure we return an object | |
return (typeof result === 'object' && result) || obj; | |
}; |
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
var domNodeArrays = Array.prototype.slice.call(domNodes); | |
// now domNodeArrays has the unshift methods | |
domNodeArrays.unshift("h1"); | |
let test = [1,2,3,4] | |
// we make a shallow copy of the orignal array, now we are isolated | |
let shallowCopy = [].slice.call(test) | |
test[1] = 4 | |
console.log(shallowCopy[1]) |
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
/** | |
* We could get the parameters with arguments in Javacript. With ES6 spread | |
* operator, you could transform it into a real list. Notes: the first | |
* parameter is the this object, so we slice it. | |
* Problems: | |
* 1. we ignore the case the context passed in is primitive type | |
* 2. we ignore the case the context passed in is null | |
* | |
* @param {Object} [context=window] | |
* @returns |
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
/** | |
* By checking the passed in context, we add an wrapper to primitive types or | |
* reset the this object to global one if null is passed in | |
* | |
* @param {*} context | |
* @returns | |
*/ | |
Function.prototype.myOwnCallUltimate = function(context) { | |
// Object will tranform primitive value into wrapper Object,kind of Java. | |
context = context ? Object(context) : window; |
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
/** | |
* Concat the parameters passed in when binding and the parameters when the new | |
* binded function takes | |
* | |
* @param {*} context | |
* @returns | |
*/ | |
Function.prototype.bindWithParam = function(context) { | |
// take the function | |
let fn = this; |
OlderNewer