0

我现在正式花了一整天时间尝试在 JavaScript 中分配一个变量!请原谅我以 4 种不同的方式提出同样的问题,但这就是我今天早上开始的内容,并且有效。我现在只需要添加第二种方法。

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);

所以我的问题是:请耐心等待,如果我添加mySecondMethodApplication,那么如何在arguments[0]不使用当前称为闭包的变量的情况下保持 的值?

4

3 回答 3

2

怎么样,它定义了一个函数,该函数接受一个字符串并返回一个 getter/setter 函数。该字符串用于指示要获取/设置值的属性,如variables.

演示

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);

如果您想在任一事件之前有一个带有自定义逻辑的 getter 或 setter,那么单独定义它们是最简单的。坚持这种this[property]模式,将所有领域都集中在一个地方。

Application.myCustomMethod = function() {
    if (arguments.length) {
        // some logic
        variables['custom'] = arguments[0];
    } else {
        // some logic
        return variables['custom'];
    }
}
于 2013-10-09T20:24:45.090 回答
1

在面向原型的编程语言术语的意义上,您似乎正在搜索向对象添加属性;只需使用“this”对象,它代表当前调用上下文,在调用方法时将设置为您的 Application 对象:

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());
于 2013-10-09T20:38:54.063 回答
0

这是我想出来的。不过,我可能需要在new某个地方使用这个词。

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);
于 2013-10-09T20:54:05.127 回答