Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Off the top of my head...</p> <p><strong>Functions</strong></p> <p>arguments.callee refers to the function that hosts the "arguments" variable, so it can be used to recurse anonymous functions:</p> <pre><code>var recurse = function() { if (condition) arguments.callee(); //calls recurse() again } </code></pre> <p>That's useful if you want to do something like this:</p> <pre><code>//do something to all array items within an array recursively myArray.forEach(function(item) { if (item instanceof Array) item.forEach(arguments.callee) else {/*...*/} }) </code></pre> <p><strong>Objects</strong></p> <p>An interesting thing about object members: they can have any string as their names:</p> <pre><code>//these are normal object members var obj = { a : function() {}, b : function() {} } //but we can do this too var rules = { ".layout .widget" : function(element) {}, "a[href]" : function(element) {} } /* this snippet searches the page for elements that match the CSS selectors and applies the respective function to them: */ for (var item in rules) { var elements = document.querySelectorAll(rules[item]); for (var e, i = 0; e = elements[i++];) rules[item](e); } </code></pre> <p><strong>Strings</strong></p> <p><em>String.split</em> can take regular expressions as parameters:</p> <pre><code>"hello world with spaces".split(/\s+/g); //returns an array: ["hello", "world", "with", "spaces"] </code></pre> <p><em>String.replace</em> can take a regular expression as a search parameter and a function as a replacement parameter:</p> <pre><code>var i = 1; "foo bar baz ".replace(/\s+/g, function() {return i++}); //returns "foo1bar2baz3" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload