0

I have a question. We all know the power of closures in Javascript and I want to use this power. Lets say I have a an object named "BRB". WHat I wanted to is whenever user calls the method getBrowser() for the very first time it will find out browser version/name whatever and return it and also store it inside itself as static when getBrowser() called second time it should return the same value without calculation since it is already statically stored somewhere. This can be done in many different ways, we can just store a property in the object and in the first call we can set some values for it and use it later, we can run getBrowser method directly when object is created in the syntax as

(function()(
...
))()

However, this is not what I want. All I want is getBrowser() method to calculate the value only once and use it all the time, I dont want to store the value inside the object somewhere else and I dont want to run this method right away when object is created, I'm allowed to use only and only this method and all action must take place in this one method. I put here an example, as you see it will always print out "0" but what I want is it prints 0,1,2,3 for each console.log request. I hope I made myself clear. Thanks.

(
function(window){

    if(window.BRB) return;

    var BRB = function(){}
    BRB.prototype.getBrowser = function(){
        var browser = null;
        return function(){
            if(browser === null){
                browser = 0;
            }
            return browser++;
        }
    }

    window.BRB = new BRB();
})(window);

console.log(BRB.getBrowser()());
console.log(BRB.getBrowser()());
console.log(BRB.getBrowser()());
console.log(BRB.getBrowser()());
1
  • I'm not sure I understand, are you okay with creating a static property on getBrowser, or not? Commented Sep 2, 2013 at 21:10

3 Answers 3

1

Your requirements are kinda strange. Is this what you're looking for? It works by creating a property on the getBrowser function itself:

(function(window){

    if(window.BRB) return;

    var BRB = function(){}
    BRB.prototype.getBrowser = function(){
        if(typeof this.getBrowser.browser == "undefined"){
            return this.getBrowser.browser = 0;
        } else {
            return ++this.getBrowser.browser;
        }
    }

    window.BRB = new BRB();
})(window);

console.log(BRB.getBrowser());
console.log(BRB.getBrowser());
console.log(BRB.getBrowser());
console.log(BRB.getBrowser());

http://jsfiddle.net/5DheZ/

Sign up to request clarification or add additional context in comments.

4 Comments

This creates another object property in prototype, which I don't need and basically similar with previous comments, but good one actually.
It doesn't create another property in the prototype, it creates a property on BRB.prototype.getBrowser.
When I think again about this answer I think it fits my needs, very well indeed. Thank you very much!
Just one thing: this will fail if you try to use it from outside BRB, so var gb = BRB.getBrowser; gb(); will throw an error.
1

You should define the browser variable in another place:

(
function(window){

    if(window.BRB) return;

    var browser = null;
    var BRB = function(){}
    BRB.prototype.getBrowser = function(){
        if(browser === null){
            browser = 0;
        }
        return browser++;
    }

    window.BRB = new BRB();
})(window);

console.log(BRB.getBrowser());
console.log(BRB.getBrowser());
console.log(BRB.getBrowser());
console.log(BRB.getBrowser());

jsfiddle http://jsfiddle.net/5ByYR/1/

And if you are able to assign an object instead of function to getBrowser:

(
function(window){

    if(window.BRB) return;

    var BRB = function(){}
    BRB.prototype.getBrowser = {
        browser: null,
        get: function() {
            if(this.browser === null){
                this.browser = 0;
            }
            return this.browser++;
        }
    }

    window.BRB = new BRB();
})(window);

console.log(BRB.getBrowser.get());
console.log(BRB.getBrowser.get());
console.log(BRB.getBrowser.get());
console.log(BRB.getBrowser.get());

8 Comments

Also there is no good reason for getBrowser to return a new function every time...
As I mentioned in question this is not allowed, I can't use this way since it prevents decoupling. If I want use this method somewhere else I must also move object properties inside another object which I dont want. Only place we are allowed to edit is getBrowser method and nothing more.
It sure doesn't have to return function, obvious. This is just dummy code, as I mentioned this can be achieved in many ways, I was just experimenting and I copied and pasted here the code.
You use a variable which lives in a scope which you create every time. There is no way to keep the value of browser without to place it outsite the scope.
The way I'm searching is, how can I create a scope that will be available only with getBrowser call but only and only in this method nowhere else, if this is not possible this is also an answer :) I'm searching for the magic answer actually.
|
1

You probably intended for the getBrowser method to be an IIFE closure for the result:

BRB.prototype.getBrowser = (function(){
    var browser = null;
    return function(){
        if(browser === null){
            browser = 0;
        }
        return browser++;
    }
})();

This way the browservariable is not reinitialized on each function call.

UPDATE
You could use a property instead of a variable scoped in a closure for the browser value:

BRB.prototype.getBrowser = function() {
    if(!this.browser){
        this.browser = 0;
    }
    return this.browser++;
}

1 Comment

This will be directly executed when object is created and as mentioned in question I dont want to do this. If I have 100 such methods they will all be executed when object is created but I dont need them to be executed at the begining.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.