Skip to content

Instantly share code, notes, and snippets.

@ismnoiet
Created July 21, 2016 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismnoiet/2045a3999c92400dcbc7c6a41decc6dd to your computer and use it in GitHub Desktop.
Save ismnoiet/2045a3999c92400dcbc7c6a41decc6dd to your computer and use it in GitHub Desktop.
// 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