What is the most efficient way to deep clone an object in JavaScript? What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency. I've also seen recursive copying functions with various flaws. I'm surprised no canonical solution exists. Answer : Note: This is a reply to another answer, not a proper response to this question. If you wish to
undefined
How do JavaScript closures work? How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I have seen the Scheme example given on Wikipedia, but unfortunately it did not help. Answer : JavaScript Closures for Beginners Submitted by Morris on Tue, 2006-02-21 10:19. Community-edited since. Closures Are Not Magic This page explains closures so that a programmer can understand them — using working JavaScript code.
undefined
Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21; is there a built-in or accepted best practice way to get the length of this object? Answer : The most robust answer (i.e. that captures the intent of what you're trying to do while causing the fewest bugs) would be: Object.size = function(obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) size++;
undefined
Detecting which font was used in a web page Suppose I have the following CSS rule in my page: body { font-family: Calibri, Trebuchet MS, Helvetica, sans-serif; } How can I detect which one of the defined fonts was used in the user's browser? Edit for people wondering why I want to do this: The font I'm detecting contains glyph's that are not available in other fonts and when the user does not have the font I want to display a link asking the
undefined
CSS background color in JavaScript How can I set the CSS background color of an HTML element using JavaScript? Answer : In general, CSS properties are converted to JavaScript by making them camelCase without any dashes. So background-color becomes backgroundColor. function setColor(element, color) { element.style.backgroundColor = color; } source : http://stackoverflow.com/questions/3319/css-background-color-in-javascript