For the case specified, if you look up
http://nodejs.org/api/fs.html#fs_fs_stat_path_callback the function
definition takes the form
fs.stat(path, [callback]) with callback in the form function(err, stats).
You'll notice that the callback has two arguments. One is the err status
and the other the result, in this case stats. Most Node.js libraries seem
to follow this convention.
Therefore you wouldn't have to use 'this' except when you use the
constructor.
The function is called passing the required parameter, in this case 'path'
and a template for the callback, something like
require('fs').stat('file.txt', function(err, result) {
console.log('Total bytes = ' + result.size);
});
will log 'Total bytes = 10' to the console. You could even log the complete
object which will output something like
{ dev: 2049,
ino: 28581379,
mode: 33188,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
size: 10,
blksize: 4096,
blocks: 8,
atime: Wed, 06 Jun 2012 13:16:22 GMT,
mtime: Wed, 06 Jun 2012 13:16:21 GMT,
ctime: Wed, 06 Jun 2012 13:16:21 GMT }
So the convention is to call a function passing the required parameters and
you get the results wrapped up in the callback.
Hope this helps.
On Wed, Jun 6, 2012 at 5:53 PM, Jérémy Lal <[email protected]> wrote:
> I don't think it tells why the result of the snippet is :
>
> { oncomplete: [Function] }
>
> I still can't tell what is this object... does node.js follow
> some convention about the context in which a function is called back
> from a library call ?
>
> Jérémy.
>
>
> On 06/06/2012 14:06, Anand George wrote:
> > This should help... http://howtonode.org/what-is-this
> >
> > On Wed, Jun 6, 2012 at 5:23 PM, Jérémy Lal <[email protected] <mailto:
> [email protected]>> wrote:
> >
> > Hi,
> >
> > require('fs').stat('file.txt', function(err, result) {
> > console.log(this); // what is this ?
> > });
> >
> > Jérémy.
> >
> > --
> > 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]<mailto:
> [email protected]>
> > To unsubscribe from this group, send email to
> > [email protected] <mailto:
> nodejs%[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
>
> --
> 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