What do you mean by not needed?  If the function uses the parameters and
won't work without them (remember all arguments are technically optional in
js), then it's needed.  If the value is already available to the function
from some other source, then either it's a badly designed API or there is
some other reason.

There are many ways to pass state around in JS, it's a very flexible
language.  The three main techniques are:

  - Treat functions as stateless and pass in all needed variables and state
as arguments
  - Generate functions that close over (closure) some common state.
  - Use object instances and "this" to store state that's available to
shared functions.

And you can mix and match these techniques.  The same function can access
passed in arguments, closed over variables, as well as the "this" value.

On Sat, Mar 31, 2012 at 5:04 AM, STT <[email protected]> wrote:

> Hello,
>
> I've just started finding my way around node.js and to do that I have
> used this http://www.nodebeginner.org/ tutorial. The author spreads
> the different modules - server, router, etc. to different files, and
> then uses require to hook them in in the main file.
>
> Now, since they are all hooked in at the begining of the way, I'm
> forced to pass arguments into functions, which these particular
> functions don't actually need. For example:
>
> route(handle, pathname, query, response, postData);
>
> I pass to the router a list of handles, a pathname, the query attached
> to it, the response object and any post data that I have received.
>
> Then the router does this:
>
> handle[pathname](response, query, postData);
>
> Which finally links to this function:
>
> function start(response, query, postData) {
>        console.log("RH start");
>        view.readFile("views/page.html","utf8",function (err,data){
>                if(err){
>                        console.log(err);
>                        return;
>                }
>                response.writeHead(200, {"Content-Type:": "text/html"});
>                response.write(data);
>                response.end();
>        });
> }
>
> It's just one of the few handlers that I have, but basically what
> start actually needed was only the response object. Is this really the
> way things are done? Because it seems a bit inefficient to me adding
> parameters that aren't actually needed.
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to