Skip to content

Instantly share code, notes, and snippets.

@ebonneville
ebonneville / cloneObject.js
Created May 22, 2012 00:17
Quickly deep-clone an object with jQuery
var bob = {
name: "Bob",
age: 32
};
var bill = $.extend(true, {}, bob);
bill.name = "Bill";
console.log(bob);
console.log(bill);
@ebonneville
ebonneville / cloneObject.js
Created May 22, 2012 00:12
Quickly deep-clone an object with JSON
var bob = {
name: "Bob",
age: 32
};
var bill = (JSON.parse(JSON.stringify(bob)));
bill.name = "Bill";
console.log(bob);
console.log(bill);
@ebonneville
ebonneville / cloneObject.js
Created May 22, 2012 00:08
Clones an object and returns the clone.
// recursive function to clone an object. If a non object parameter
// is passed in, that parameter is returned and no recursion occurs.
function cloneObject(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
var temp = obj.constructor(); // give temp the original obj's constructor
for (var key in obj) {
myString = (myString == "true");
var myString = "true";
if(myString == "true") {
// this evaluates to true correctly, myString is true
}
if(myString == "false") {
// this evaluates to false, also correct, since myString doesn't equal false.
}
var myString = "true";
if(Boolean(myString)) {
// very good, this if statement evaluates to true correctly!
}
if(!Boolean(myString)) {
// and this one evaluates to false! Our problems are solved!
}
var myString = true;
if (myString) {
// this should evaluate to true because myString = "true", and it does. Good!
}
if (!myString) {
// uh oh! This evaluates to true as well. Why? Because if(!myString)
// is checking to see if myString *exists*, not if it's *true*.
}
myString = (myString == "true");
var myString = "true";
if(myString == "true") {
// this evaluates to true correctly, myString is true
}
if(myString == "false") {
// this evaluates to false, also correct, since myString doesn't equal false.
}
var myString = "true";
if(Boolean(myString)) {
// very good, this if statement evaluates to true correctly!
}
if(!Boolean(myString)) {
// and this one evaluates to false! Our problems are solved!
}