var vs this in Javascript object

后端 未结 4 733
[愿得一人]
[愿得一人] 2020-11-29 09:33

I\'m developing a web framework for node.js. here is the code;

function Router(request, response) {
        this.routes = {};

        var parse = require(         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 10:26

    You can think of properties hung from this sort of like instance variables in other languages (sort of).

    It looks like you're creating a constructor function and will likely add some prototype methods. If that's the case, and you need access to routes, you've got it, but not path.

    Router.prototype = {
      doSomething: function(){
        this.routes; // available
        path; // not available
      }
    }
    

提交回复
热议问题