Showing posts with label Core JavaScript. Show all posts
Showing posts with label Core JavaScript. Show all posts

Sunday, February 16, 2014

JavaScript Execution Contexts

1 - Execution Context or Context


The execution context or context is the environment where JavaScript code is evaluated.
→ Global (execution) context
The global execution context is the most outer execution context.
In browsers the global context is also known as window context. In a browser global variables are window properties and global functions are window methods. 
→ Function or Local (execution) context
When a function is called a function execution context is created.

Friday, September 7, 2012

JavaScript Object's Property Printer

A JavaScript object is a collection of properties. You can think of a property as a pair (key : value).
To inspect an object in JavaScript you use the for in operator.

The following function inspects an object by looping on the properties and calling itself recursively.

var objectInspector = function recursiveObjectInspector(obj) {
    var string = '';
    for (var propName in obj) {
        if(typeof(obj[propName]) !== 'object')
            string += propName+" : "+obj[propName]+",\n";
        else
            string += '\n'+propName+' {\n'+ recursiveObjectInspector(obj[propName])+"}\n";
    }
    return string;
};

See the Pen object property inspector by Massimiliano De Simone (@maxdesimone) on CodePen.