Last active
August 29, 2015 14:23
-
-
Save rhys-vdw/ac4a2928ec828ea116b4 to your computer and use it in GitHub Desktop.
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 Irritating() { | |
return function() { | |
return function() { | |
return function() { | |
this.name = 'thing'; | |
} | |
} | |
} | |
} | |
(new new new new Irritating()).name // === 'thing' | |
// Let's make this easier. | |
function deepNew(Constructor, depth) { | |
var instance = new Constructor; | |
for (var i = 1; i < depth; i++) { | |
instance = new instance; | |
} | |
return instance; | |
} | |
deepNew(Irritating, 4).name // === 'thing' | |
// Much better! | |
// Now, how about that declaration... Let's wrap it in a factory for future convenience. | |
function deepConstructor(properties, depth) { | |
if (depth < 1) { | |
throw new Error('`depth` must be >= 1'); | |
} | |
var innerMost = function() { | |
for (property in properties) { | |
this[property] = properties[property]; | |
} | |
}; | |
return depth === 1 ? innerMost : function() { | |
return deepConstructor(properties, depth - 1) | |
}; | |
} | |
// Okay, great. Ready to go! | |
instance = deepNew(deepConstructor({thisIs: 'SO MUCH EASIER!'}, 5000)); | |
instance.thisIs // === 'SO MUCH EASIER!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment