node.js - var vs this in Javascript object -
i'm developing web framework node.js. here 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 change var this, 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?
add properties this
when want properties persist life of object in question. use var
local variables.
edit — bergi notes in comment, variables declared var
don't necessarily vanish upon return function invocation. are, , remain, directly accessible code in scope in declared, , in lexically nested scopes.
Comments
Post a Comment