jeudi 12 novembre 2015

Is this a JavaScript constructor pattern?

I've been using this pattern in my JS code:

function Thing() {
    var otherData = {
        // Private variables?
        name : "something"
    }

    var myThing = {
        data: "somedata",
        someFunction: function () {
        console.log(otherData.name);
        }
    }

    return myThing;
}

Then when using it doing:

var thing = Thing();
thing.someFunction();

I've seen examples of constructors and singletons in JS, but I haven't run across this pattern before. Is there a name for this pattern? Are there any potential problems with this pattern? Previously I was just using the object literal pattern, but wanted to get private-ish variables by putting it in a closure.

Aucun commentaire:

Enregistrer un commentaire