mardi 21 mars 2017

What's the diference between this two functions declarations in Javascript?

I have some doubt about this two javascript case:

function some() {
    this.some2 = function() {//this is associated with own object(Correct?)
        console.log('some2');
    }
}

var __s1 = new some();
__s1.some2();//Allright, this do the work;
//some().some2();//This not work

This declaration type has a technical name? Object patterns maybe?

Another similiar case is this:

function some() {
    return {
        some2: function() {
            console.log('some2');
        }
    }
}

var __s2 = new some();
__s2.some2(); // Right, this work;
some().some2(); //Also work ... Ok, "some()" returns a object that contain
                //the some2().

This second case, also has a technical name?

What's best case between they?

Aucun commentaire:

Enregistrer un commentaire