2

I'm refactoring some piece of code:

with (form.proxy_settings) {
      PutValue("Usage", 1);
      PutValue("Server", 2);
      PutValue("ServerPort", 3);
      PutValue("Login", 3);
}

I want to remove with. Should I do like that?

(function() {
      ps.PutValue("Usage", 1);
      ps.PutValue("Server", 2);
      ps.PutValue("ServerPort", 3);
      ps.PutValue("Login", 3);
}(form.proxy_settings);

Or is it better in this particular case to simply add form.proxy_settings to where it is required?

4

1 回答 1

2

首先,如果使用函数,则需要声明参数ps

(function(ps) {
      ps.PutValue("Usage", 1);
      ps.PutValue("Server", 2);
      ps.PutValue("ServerPort", 3);
      ps.PutValue("Login", 3);
}(form.proxy_settings);

但是,您实际上并不需要执行函数。这同样适用,并且是一种常见的 javascript 设计模式,与在每行中使用较长的名称相比,可以节省输入并提高性能:

var ps = form.proxy_settings;
ps.PutValue("Usage", 1);
ps.PutValue("Server", 2);
ps.PutValue("ServerPort", 3);
ps.PutValue("Login", 3);
于 2013-11-14T17:41:27.030 回答