JSFiddle - React, Tailwind, and code Playground
by gentooboontoo
HTML
<script src="https://rawgithub.com/lodash/lodash/2.4.1/dist/lodash.js"></script>
JavaScript
function A () {this.foo = "bar";}
A.prototype.method = function () {};
var refObj = {bar: "baz"};
function testObj () {
return {
"number": 42,
"string": "foo",
"boolean": true,
"array": [1,2,function(){}],
"object": {foo: "bar"},
"objRef1": refObj,
"objRef2": refObj,
"arguments": arguments,
"function": function () {},
"instance": new A(),
"date": new Date(),
"regexp": /^$/g,
"math": Math,
"nan": NaN,
"null": null,
"infinity": Infinity
};
}
function myclone(source) {
var destination = Object.create(Object.getPrototypeOf(source));
var ownProperties = Object.getOwnPropertyNames(source);
ownProperties.forEach(function(key) {
var propertyDescriptor = Object.getOwnPropertyDescriptor(source, key);
if(propertyDescriptor.hasOwnProperty('value')) {
var value = propertyDescriptor.value;
propertyDescriptor.value = (!!value && typeof value === 'object') ? myclone(value) : value;
}
Object.defineProperty(destination, key, propertyDescriptor);
});
return destination;
}
source = testObj(1, "e", {f: "g"});
copy = myclone(source);
dashcopy = _.clone(source);
console.log(source, copy, dashcopy);