0

I was wondering how can I make it posible to get rid of putting "new" before a function, for example:

new functionToDo("thingsToDo").iGotYouBruh("Halo Humans");

is there a posible way of doing this without the "new"?

here is the code I'm trying to use without the "new":

function local (title) {
    var storeTitle = title;
    this.addL = function(lString) {
        var storeText = lString;
        localStorage.setItem(storeTitle, storeText);
        console.info("Locally stored " + storeTitle.toUpperCase() + " with " + storeText.substring(0, 10) + "... As text.");
    };
    this.removeL = function() {
        localStorage.removeItem(storeTitle);
        console.info("Locally removed " + storeTitle + ".");
    };
    this.getL = function () {
        localStorage.getItem(storeTitle);
        console.info("Locally got string of " + storeTitle + ": " + localStorage.getItem(storeTitle));
    };
};

and here's what I would have to do to invoke the function:

new local("storedElement").getL();
UzendayoNE
  • 161
  • 1
  • 12
  • 1
    I mean is it really that big of a deal? – Ryan Feb 07 '15 at 00:56
  • 1
    I believe you're referring to [Object.create()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create)? – Tim Vermaelen Feb 07 '15 at 00:57
  • @self for me it was, now that it's solved I will make some research on the answer and learn from it. – UzendayoNE Feb 07 '15 at 00:58
  • @TimVermaelen Yes, I am refering to that and now I see there's already a post about this here [link](http://stackoverflow.com/questions/2709612/using-object-create-instead-of-new) – UzendayoNE Feb 07 '15 at 01:04

2 Answers2

3

This is possible by checking whether this is an instance of the function itself and returning a new instance otherwise:

function local (title) {
    if (!(this instanceof local)) {
        return new local(title);
    }

    var storeTitle = title;
    this.addL = function(lString) {
        var storeText = lString;
        localStorage.setItem(storeTitle, storeText);
        console.info("Locally stored " + storeTitle.toUpperCase() + " with " + storeText.substring(0, 10) + "... As text.");
    };
    this.removeL = function() {
        localStorage.removeItem(storeTitle);
        console.info("Locally removed " + storeTitle + ".");
    };
    this.getL = function () {
        localStorage.getItem(storeTitle);
        console.info("Locally got string of " + storeTitle + ": " + localStorage.getItem(storeTitle));
    };
};
TimWolla
  • 31,849
  • 8
  • 63
  • 96
1

You could use JavaScript closures. In particular look at the "Using Closures for the Module Pattern" section of this webpage for a full description. The idea is to have the function return an literal with all the required methods. Any functions or variables that you want to be kept private are just local variables for the function.

  • That helps but I don't get it, how would I make it work with only `local(title).getL()`? If you could explain it a bit you will be THE Master :) – UzendayoNE Feb 07 '15 at 01:25