mercredi 31 août 2016

Simple Closure example in Module Pattern not working

In the simple example I want to demonstrate:

1. Module Pattern
2. Closure 

I am trying to change a value of Local Variable by making use of closure. But, I cannot get it implemented (Output must be 15 in console).

HTML

<script>
function box(){
    var a;
    a=10;

    var obj = {};
    obj.func1 = function(b){
        a = a+b;
        console.log(a);
    }
    return obj;
}

box.func1(5);
</script>

Please, help me out in the example.

Also, tell me have I missed any key componenet of Module Pattern in this simple example?

Aucun commentaire:

Enregistrer un commentaire