Created
July 21, 2016 10:25
-
-
Save ismnoiet/2045a3999c92400dcbc7c6a41decc6dd 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
// A function for creating a new object that inherits from another | |
// method1: | |
// naked function to be used as a constructor | |
var Ctor = function(){}; | |
function cloneObj(srcObj){ | |
Ctor.prototype = srcObj; | |
var clone = new Ctor(); | |
Ctor.prototype = null; | |
return clone; | |
} | |
/* | |
example : | |
var obj1 = { | |
name:'something', | |
test:function(){console.log('test method called!');} | |
}; | |
var obj2 = cloneObj(obj1); | |
obj2.test(); // > test method called! | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment