在JavaScript中为应用程序分配多个方法

在JavaScript中为应用程序分配多个方法,javascript,getter-setter,Javascript,Getter Setter,我现在正式花了一整天的时间试图在JavaScript中分配一个变量! 请原谅我用四种不同的方式问了同样的问题,但这是我今天早上开始问的问题,这很有效。我现在只需要添加第二个方法 Application = {}; (function() { var closure = {}; Application.myFirstMethod = function() { if (arguments.length) { closure = argumen

我现在正式花了一整天的时间试图在JavaScript中分配一个变量! 请原谅我用四种不同的方式问了同样的问题,但这是我今天早上开始问的问题,这很有效。我现在只需要添加第二个方法

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

所以我的问题是:请耐心等待,如果我将
mySecondMethod
添加到
Application
,那么如何在不使用当前称为closure的变量的情况下保留
参数[0]
的值呢?

如何,它定义了一个接受字符串并返回getter/setter函数的函数。字符串用于指示在
变量中获取/设置值的属性

如果您想在任何一个事件之前拥有一个带有自定义逻辑的getter或setter,那么只需单独定义它们就很容易了。坚持使用
this[property]
模式将所有字段保持在一个位置

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

那么,它定义了一个函数,该函数接受一个字符串并返回一个getter/setter函数。字符串用于指示在
变量中获取/设置值的属性

如果您想在任何一个事件之前拥有一个带有自定义逻辑的getter或setter,那么只需单独定义它们就很容易了。坚持使用
this[property]
模式将所有字段保持在一个位置

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

那么,它定义了一个函数,该函数接受一个字符串并返回一个getter/setter函数。字符串用于指示在
变量中获取/设置值的属性

如果您想在任何一个事件之前拥有一个带有自定义逻辑的getter或setter,那么只需单独定义它们就很容易了。坚持使用
this[property]
模式将所有字段保持在一个位置

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

那么,它定义了一个函数,该函数接受一个字符串并返回一个getter/setter函数。字符串用于指示在
变量中获取/设置值的属性

如果您想在任何一个事件之前拥有一个带有自定义逻辑的getter或setter,那么只需单独定义它们就很容易了。坚持使用
this[property]
模式将所有字段保持在一个位置

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

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

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

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

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

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

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

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

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

这是我的想法。不过,我可能需要在某个地方使用单词
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);

这是我的想法。不过,我可能需要在某个地方使用单词
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);

这是我的想法。不过,我可能需要在某个地方使用单词
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);

这是我的想法。不过,我可能需要在某个地方使用单词
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);


我想我可以复制/粘贴IIFE,但是如果我需要跨方法共享变量,我该怎么办?定义另一个变量..?是的,但这似乎不正确。myFirstClosure和mySecondClosure?什么是
local
呢?好吧,
closure
是一个对象,为什么不声明
closure.varFromFirstMethod=arguments[0]
closure.varFromSecondMethod=参数[0]我想我可以复制/粘贴IIFE,但如果我需要跨方法共享变量,我该怎么办?定义另一个变量..?是的,但这似乎不正确。myFirstClosure和mySecondClosure?什么是
local
呢?好吧,
closure
是一个对象,为什么不声明
closure.varFromFirstMethod=arguments[0]
closure.varFromSecondMethod=参数[0]我想我可以复制/粘贴IIFE,但如果我需要跨方法共享变量,我该怎么办?定义另一个变量..?是的,但这似乎不正确。myFirstClosure和mySecondClosure?什么是
local
呢?好吧,
closure
是一个对象,为什么不声明
closure.varFromFirstMethod=arguments[0]
closure.varFromSecondMethod=参数[0]我想我可以复制/粘贴IIFE,但如果我需要跨方法共享变量,我该怎么办?定义另一个变量..?是的,但这似乎不正确。myFirstClosure和mySecondClosure?什么是
local
呢?好吧,
closure
是一个对象,为什么不声明
closure.varFromFirstMethod=arguments[0]
closure.varFromSecondMethod=参数[0]非常有趣。现在,需要两个方法是因为它们都有自己的逻辑,所以您构建1个makeGetterSetter方法的解决方案有点偏离目标,但我想我看到了我的不足之处。我需要返回一个返回值的函数。不清楚您实际要完成的是什么,似乎您正试图使JavaScript遵循它不是为之设计的模式。为qu添加更多上下文