summaryrefslogtreecommitdiffstats
path: root/anon-func.js
blob: c4e313027d89969b362a740963b2b48f267627aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* this is an error
function() {
    console.log("hello");
}
*/

// current context in this outermost scope
var c = " foobar";

// prints nothing, code doesn't execute
(function() {
    console.log('hello' + c);
});

// () at the end executes declared function
// NOTICE that functions are objects, better yet, first-class objects, since
// they can be assigned to a variable
(function() {
    console.log('hello' + c);
})();

// prints hello again foobar
(function() {
    console.log('hello again' + c);
}).call(this);

// prints hello again cruel world foobar
(function(par1, par2) {
    console.log('hello again' + par1 + par2 + c);
}).apply(this, [" cruel", " world"]);

// prints 7 and 13
var f = function() { return 7; };
var g = (function() { return 13; });
console.log(f());
console.log(g());

(function() {
    // "use strict" cannot be used since 'this' is an undeclared object, it
    // exists but only for life of its scope
    this.x = "barfoo";
    console.log(x);
    console.log(this.x);

    (function() {
        // we're lucky here since js searches for x in higher scopes
        console.log(x);
        console.log(x);
    })();
})();