Question

I have now officially spent all day trying to assign a variable in JavaScript! Forgive me for asking this same question 4 different ways, but here's what I started out with this morning, and this works. I just need to add a second method to it now.

Application = {};
(function() {
    var closure = {};

    Application.myFirstMethod = function() {
        if (arguments.length) {
            closure = arguments[0];
        } else {
            return closure;
        }
    }
})();

Application.myFirstMethod(3.14);
result = Application.myFirstMethod();
log(result);

So my question is: and please be patient with me, if I add mySecondMethod to Application, then how do I keep the value of arguments[0] without using the variable that is currently called closure?

Was it helpful?

Solution 3

Here's what I figured out. I probably need to use the word new somewhere though.

Application = {};
(function() {
    Application.myFirstMethod = FirstMethod();
    Application.mySecondMethod = SecondMethod();

    function FirstMethod() {
        var closure = {};
        return function(myArgument) {
            if (arguments.length) {
                closure.result = arguments[0]; // myArgument
            } else {
                return closure.result;
            }
        }
    }
    function SecondMethod() {
        var closure = {};
        return function(myArgument) {
            if (arguments.length) {
                closure.result = arguments[0]; // myArgument
            } else {
                return closure.result;
            }
        }
    }
})();


Application.myFirstMethod(3.14);
result = Application.myFirstMethod();
log(result);

Application.mySecondMethod(2013);
result = Application.mySecondMethod();
log(result);

OTHER TIPS

How about this, it defines a function that takes a string and returns a getter/setter function. The string is used to indicate what property to get/set the value as in variables.

Demo

Application = {};
(function() {
    var variables = {};

    Application.myFirstMethod = makeGetterSetter('myFirst');
    Application.mySecondMethod = makeGetterSetter('mySecond');

    function makeGetterSetter(name) {
        return function () {
            if (arguments.length) {
                variables[name] = arguments[0];
            } else {
                return variables[name];
            }
        };
    }
})();

Application.myFirstMethod(4);
result1 = Application.myFirstMethod();
Application.mySecondMethod(5);
result2 = Application.mySecondMethod();
console.log(result1);
console.log(result2);

If you wanted to have a getter or setter with custom logic in it before either event then it would be easiest to just define them separately. Stick with the this[property] pattern to keep all your fields in one spot.

Application.myCustomMethod = function() {
    if (arguments.length) {
        // some logic
        variables['custom'] = arguments[0];
    } else {
        // some logic
        return variables['custom'];
    }
}

It looks like you are searching for adding properties to objects, in the Prototype-Oriented Programming Language sense of the term; just use the "this" object, which stands for the current calling context, and which will be set to your Application object when calling the methods:

Application = {};
(function() {

  Application.myFirstMethod = function() {
    if (arguments.length) {
        this.foo = arguments[0];
    } else {
        return this.foo;
    }
  };
  Application.mySecondMethod = function() {
    if (arguments.length) {
        this.bar = arguments[0];
    } else {
        return this.bar;
    }
  };
})();

Application.myFirstMethod(3.14);
console.log(Application.myFirstMethod());

Application.mySecondMethod(2097);
console.log(Application.mySecondMethod());
console.log(Application.myFirstMethod());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top