(function() { 
  // Function body
 }) ();

function() { } defines a javascript function as is obvious. Surrounding it with () makes it an expression. Following it with () executes it. So, this is a self-executing anonymous function where you define a function and force executing it immediately. This kind of construct provides scoping for your executing function, so it doesn’t pollute the global Javascript namespace.

This technique is called a Closure and it is used to allow a function to execute within it’s own Private Scope that does not pollute the global namespace.
Additionally, functions defined within the closure cannot be accessed from methods outside of the closure and thus it is a nifty way in JavaScript to provide scope security of code.

If you’re using a library like jQuery or prototype it will protect the use of shorthand items (like « $ »).

For example:

		if(window.console&&window.console.log) { 
			console.log('I play nice with other JavaScript libraries!');

Eloquent Javascript:

=== and !== Operators.
Use the === and !== operators. The == and != operators do type coercion and should not be used.

Using closure to achieve ‘information hiding’
JavaScript has no provision for private object literals. Everything is ‘public’ . But thanks to closure, we can do:

myValAccessor = (function() {
var val = 0; //this is forever hidden from access from the outside
return {
getVal : function() { return val; }
setVal : function(newVal) { val = typeof newVal == 'number' ? newVal : val; }

Variables conversions:

var myVar   = "3.14159",  
    str     = ""+ myVar,//  to string  
    int     = ~~myVar,  //  to integer  
    float   = 1*myVar,  //  to float  
    bool    = !!myVar,  /*  to boolean - any string with length 
                            and any number except 0 are true */  
    array   = [myVar];  //  to array  

Using his browser like et notepad:
content editable

data:text/html, html  >

Check all global variables by just console logging:


Test for IE:


You can set the URL bar in the browser to something else. This is useful if you are calling an internal URL in your application, but want that your users use another URL for public sharing.

function setURLBar(theurl) {
    history.pushState({}, '', theurl);

Lazy loading technique for checking a function existing and then executing it:

obj.function && obj.function();

There’s an operator called const that keeps a variable set in stone no matter what:

    const a = 10;
    a = 20;
    // alert(a); // 10

MutationObserver provides developers a way to react to changes in a DOM. It is designed as a replacement for Mutation Events defined in the DOM3 Events specification.

// select the target node
var target = document.querySelector('#some-id');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing

You can read the text at any XY coordinate on a webpage:

var x = 50, y = 100;
var range = document.caretRangeFromPoint(x, y);
if(range) {
    var word = range.startContainer.textContent.substring(range.startOffset, range.endOffset);

To make URLs automatically load from either ‘http://’ or ‘https://’ based on the current protocol, you can write them like this:


Node.js Memory Leak


A tour of V8: Garbage Collection:

Related Posts

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *