2011/2/24 Adrian Berg <[email protected]>: > I'm new to Javascript, and I'm wanting to write a simple program that > asks the user to state what it is they want calculated, "one plus one" > for instance. Then I would like the program to store that information > and use it as instructed. > > process.stdin.resume(); > process.stdin.setEncoding('utf8'); > > process.stdin.on('data', function (chunk) { > process.stdout.write('data: ' + chunk); > }); > > process.stdin.on('end', function () { > process.stdout.write('end'); > });
Line 4 starts listening to 'data' events on stdin and when a 'data' event happens it calls the function. Line 5 writes to stdout. Line 8 starts listening to 'end' events on stdin and when a 'end' event happens it calls the function. Line 9 writes to stdout. > I understand line 2, but I'm confused about the rest, and was hoping > someone might be willing to help me understand this. Also, from node- > repl, you can type in process and then press enter to get a list of > stuff. I don't know what you would call them, and I'm not quite sure > how to manipulate all of them. It firsts gives you some environment > information and then this: That is a variable dump. If you know JavaScript you can know how to manipulate and access object properties. There are 2 ways to access object properties. object.property or object['property'] In JavaScript you can assign new values to them freely, although it may cause issues if you manipulate some key properties, maybe the application will not function properly later on. > I understand that you can use most of these, but I'm not sure how. I > know stdin is used by typing process.stdin<something>. I also don't > understand why stdin is not considered a function. stdin is an object, it has events and methods and a bunch of other properties. Why would it be good if it would be a function? > How do you use the functions, _readImpl and _writeImple for instance. > _writeWatcher and _readWatcher both take two arguments that look like > key:value pairs. Most probably you should not use _writeWatcher and _readWatcher functions, as they are 'private'. Just use the functions that are available in the documentation. -- Poetro -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
