8

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

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

        var parse = require('url').parse;

        var path = parse(request.url).pathname,
            reqRoutes = this.routes[request.method],
            reqRoutesLen = reqRoutes.length;

        ..... // more code

};

Should I change all the var to this, like so:

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

        this.parse = require('url').parse;

        this.path = this.parse(request.url).pathname;
        this.reqRoutes = this.routes[request.method];
        this.reqRoutesLen = this.reqRoutes.length;

        ..... // more code

};

Any comments?

Tat-Yuen Hui
  • 141
  • 1
  • 6
  • 3
    This is a pretty fundamental difference. Are you sure you know what `this` is and what local variables are? –  Feb 09 '11 at 15:06
  • I have C++ background, Javascript feel strange to me. – Tat-Yuen Hui Feb 09 '11 at 15:11
  • In that case, are you developing a web framework just for fun or learning? Frameworks like express might be a better choice for your production code while you are learning. – 7zark7 Apr 04 '13 at 13:56
  • possible duplicate of [Javascript: Do I need to put this.var for every variable in an object?](http://stackoverflow.com/q/13418669/1048572) – Bergi May 18 '14 at 08:59

4 Answers4

13

Add properties to this when you want the properties to persist with the life of the object in question. Use var for local variables.

edit — as Bergi notes in a comment, variables declared with var don't necessarily vanish upon return from a function invocation. They are, and remain, directly accessible only to code in the scope in which they were declared, and in lexically nested scopes.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Notice that local variables can persist with the life of the object as well if referenced from a closure. – Bergi Apr 04 '13 at 13:08
1

It on depends what you want to do.

If you declare the variables with var, then they are local to the function and cannot be accessed outside.

If you assign the variables to this, then they will be set as properties of the context object the function is called on.

So if e.g. if you write:

var obj = new Router();

then obj will have all the variables as properties and you can changed them. If you call

somobject.Router()

then all the variables will be set as properties of someobject.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

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
  }
}
Ryan Florence
  • 13,361
  • 9
  • 46
  • 63
1

Using var in the constructor is usually used for private variable while using this. is used for public variable.

Example with this. :

function Router() {
    this.foo = "bar";

    this.foobar = function () {
         return this.foo;
    }
}

var r = new Router();
r.foo // Accessible

Example with var :

function Router() {
    var _foo = "bar";

    this.foobar = function () {
         return _foo;
    }
}

var r = new Router();
r._foo // Not accessible
radarbob
  • 4,964
  • 2
  • 23
  • 36
HoLyVieR
  • 10,985
  • 5
  • 42
  • 67