Skip to content

Instantly share code, notes, and snippets.

View ositowang's full-sized avatar
👨‍💻
10000 hours in progress

Osito_Wang ositowang

👨‍💻
10000 hours in progress
  • Massachusetts
View GitHub Profile
@ositowang
ositowang / newOperator.js
Last active April 1, 2019 20:06
simulating the new operator in javascript
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);
@ositowang
ositowang / ultimateDeepClone.js
Created April 1, 2019 01:22
Ultimate version solving circular dependency with weakMap
/**
* 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()) => {
@ositowang
ositowang / betterDeepClone.js
Created April 1, 2019 01:16
More comprehensive deepclone with JS. Cover null and Array.
/**
* 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
@ositowang
ositowang / simplestDeepClone.js
Created April 1, 2019 01:11
Maybe good enough for interview Simpliest Version of deepClone.
/**
* 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) => {
@ositowang
ositowang / jsonDeepClone.js
Created March 30, 2019 22:37
Object Deep Clone with Javascript
var newObj = JSON.parse( JSON.stringify( someObj ) );