Closure:

(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:

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

Eloquent Javascript: http://eloquentjavascript.net/print.html

=== 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:

keys(window);

Test for IE:

IE=!+"\v1"

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) {
    console.log(mutation.type);
  });    
});
 
// 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
observer.disconnect();

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) {
    range.expand('word');
    var word = range.startContainer.textContent.substring(range.startOffset, range.endOffset);
    console.log(word);
}

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

src="//domain.com/path/to/script.js"

Node.js Memory Leak

https://www.joyent.com/blog/walmart-node-js-memory-leak

v8.getHeapSpaceStatistics():
https://nodejs.org/api/v8.html#v8_v8_getheapspacestatistics

A tour of V8: Garbage Collection:
http://jayconrod.com/posts/55/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 *