【问题标题】:Javascript effective code structure [closed]Javascript有效的代码结构[关闭]
【发布时间】:2012-08-21 20:23:29
【问题描述】:

什么是最好的(性能,内存方面)实现一个干净的方式来创建一个类或某种方式来正确地构造代码,共享 2 个变量(req,res),这些变量具有合适的对象大小。

是的,对于那些使用 Node.js 的人来说,它是 req 和 res 变量,但这无关紧要。

这是我迄今为止尝试过的:

function Client(req, res){
  var self = this;

  self.req = req;
  self.res = res;

  route.call(self);
  handleRoute.call(self);
  handleRequestData.call(self);
}

function route(){
  var self = this;

  self.req.route = // ...
}

function handleAuth(){
  var self = this;

  self.req.auth = // ...
}

function handleRequestData(){
  var self = this;

  self.req.data = // ...
}

我想知道它是否可以改进,因为我提到 req 和 res 是具有方法和属性的相当不错的对象。既然 .call(self) 你通过实例,那会是最有效的方法吗?

我也不喜欢使用“var self = this;”一直以来,它都是无用的。

顺便说一句,我不想​​使用 Coffeescript。

解决方案1.(由于大量通过req和res而失败)

假设我们得到了文件1。客户端.js

var Client = {
  route: function(req, res){},
  auth: function(req, res, callback){ callback(req, res); },
  getData: function(req, res, callback){ callback(req, res); } // async
}

文件2. main.js

var client = require('./client.js');

client.route(req, res); // fine because this is syncronous.

// This is where the problem is, massive passing through req and res parameters.
client.auth(req, res, function(req, res){
  client.getData(req, res, function(req, res){
    // Do something...
  });
});

【问题讨论】:

    标签: javascript oop javascript-objects


    【解决方案1】:

    我不知道为什么你不断地将上下文 (this) 保存在局部变量 (self) 中。你不妨一直使用this,除非你需要将它带入不同范围的本地对象中。

    此外,您可以链接 Prototype 中的其他方法以访问其中的上下文:

    function Client(req, res){
    
      this.req = req;
      this.res = res;
    
      this.route();
      this.handleRoute();
      this.handleRequestData();
    }
    
    Client.prototype.route = function(){
      this.req.route = // ...
    }
    
    Client.prototype.handleAuth = function(){
      this.req.auth = // ...
    }
    
    Client.prototype.handleRequestData = function(){
      this.req.data = // ...
    }
    

    如果您不想要“公共”方法,请执行以下操作:

    function Client(req, res){
    
      this.req = req;
      this.res = res;
    
      route.call(this);
      handleRoute.call(this);
      handleRequestData.call(this);
    }
    
    function route(){
      this.req.route = // ...
    }
    
    function handleAuth(){
      this.req.auth = // ...
    }
    
    function handleRequestData(){
      this.req.data = // ...
    }
    

    【讨论】:

    • 私有方法?构造函数进行级联,我不想能够调用 client.handleAuth();
    • 好的,你应该提一下。顺便说一句,这并不能解释您为什么将 this 放入 self...
    • 有些函数是异步的,重新定义了“this”变量,但我还是希望能够引用类的实际实例。
    • 好吧,我猜这取决于每个函数来处理它的上下文。您总是可以将其作为参数传递,以节省一些额外的代码行。
    【解决方案2】:

    您是否打算重用routehandleAuthhandleRequestData 函数?它们可能是 Client 构造函数的“私有”:

    function Client(req, res) {
        function route() {
            //...
        }
    
        function handleAuth() {
            //...
        }
    
        function handleRequestData() {
            //...
        }
    
        route();
        handleRoute();
        handleRequestData();
    }
    

    看到我没有将reqres 设置为this 成员。我不知道在你的情况下这是否是强制性的。此外,这只是可以做的事情的开始;如该答案的 cmets 所述,每次创建新的 Client 时,也会创建私有函数的新实例,从而浪费资源。

    更复杂的方法可以使用自调用函数表达式定义Client

    var Client = (function() {
        function route(req, res) {
            //...
        }
    
        function handleAuth(req, res) {
            //...
        }
    
        function handleRequestData(req, res) {
            //...
        }
    
        // This is the constructor function:
        return function(req, res) {
            route(req, res);
            handleRoute(req, res);
            handleRequestData(req, res);
        }
    })();
    

    这里,Client 定义为用最外圆括号括起来的函数表达式的乘积。函数表达式返回构造函数,它可以访问闭包函数routehandleRoutehandleRequestData。请注意,这些函数中的每一个都是使用reqres 参数定义的。这样,它们的行为就像静态私有函数一样,无论this 引用什么,您都可以编码和使用它们。

    关于 self = this: 众所周知,JavaScript 可能对 this 关键字非常困惑。 Some information here。因此,有时将this 分配给名为selfme 的闭包变量会很方便。不过,它不应该盲目地做……毕竟。

    【讨论】:

    • 如果您计划创建多个客户端对象,我不建议这样做。路由、handleAuth 等函数会有多个副本,每个实例一个。大卫的建议会更好地解决这个问题。
    • 你是对的,@ScottSauyet。我发布的代码可能不适合生产。我会扩展我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多