Scott,
The way you're doing it is exactly right. The "instanceof" guard is
sadly necessary, since there's no way to specify that a function may
*only* be called as a constructor in JS, and calling it without new
will then pollute the global namespace, and there'll be a reward on
your head for $NaN.
To do classic inheritance, it's pretty easy. require("util").inherit
is used a lot in node-core. I'm more a fan of my own userland
"inherits" lib, because it's a little more flexible, but that comes at
the cost of being pretty bloated. (It's the same line count, but it's
7 statements instead of only 2.)
An OOP helper library that is longer than 20 lines of code is at least
double the size it should be. Get it done, and then move on. Don't
waste too much time farting around with stuff like this.
On Tue, Apr 17, 2012 at 06:13, Scott Ware <[email protected]> wrote:
> Thanks, Tim...Good advice! I came across those articles the other day and
> bookmarked them. I'll be sure to read them!
>
> On Tuesday, April 17, 2012 9:07:57 AM UTC-4, Tim Caswell wrote:
>>
>> The question isn't entirely off-topic. The type of code you're writing
>> affects the style of code you write. In particular prototype based objects
>> are one of the techniques to make async callbacks organized.
>>
>> On howtonode, I've written a few articles explaining the various OOP
>> techniques in JavaScript.
>>
>> http://howtonode.org/object-graphs
>> http://howtonode.org/object-graphs-2
>> http://howtonode.org/object-graphs-3
>>
>> More recently, I wrote an article about reading and parsing linux device
>> files. The article is written using vanilla callbacks. At the end, I
>> convert it to OOP style using constructor+prototype.
>>
>> http://nodebits.org/linux-joystick
>>
>> Also I'll note that constructor+prototype is *very* fast in V8. The
>> closure factory style is nice in event based code because you don't need to
>> worry about binding "this" as is often a problem with passing callbacks
>> around.
>>
>>
>> On Tue, Apr 17, 2012 at 6:53 AM, Scott Ware <[email protected]> wrote:
>>>
>>> Hello, Node.js community!
>>>
>>> I've been using Node.js for about 8 months now, and have been constantly
>>> learning as much as I can about it as well as bettering my knowledge on JS
>>> as well. Its a fantastic product, and can't remember the last time I had
>>> this much fun hacking away at things!
>>>
>>> My question is in regards to OOP: Is there a "standard" or specific
>>> method that most of the community uses or is it mainly all just personal
>>> preference? I've been browsing as much code as I can on Github, looking at
>>> the different methods and what not. Such as Pseudo-classical, Prototypal,
>>> etc.
>>>
>>> Currently I include this in my constructor(s):
>>>
>>> function Tester(name) {
>>> if (!(this instanceof Tester)) {
>>> return new Tester(name);
>>> }
>>>
>>> this.name = name;
>>> }
>>>
>>> Tester.prototype.say = function () {
>>> console.log('Hello, ' + this.name);
>>> }
>>>
>>> var person = new Tester('Scott');
>>> person.say();
>>>
>>> To that, I have read the "Good Parts" book and some others, and also
>>> wonder if there is a style that's preferred based on certain methods or
>>> use-cases that might be deprecated or soon will be? Like getting away from
>>> the using 'new' style of coding.
>>>
>>> Sorry if this is such a n00b question. When I write code, programs, I
>>> just want to make sure that I have done my research and am doing it the
>>> right way, vs a "wrong?" way.
>>>
>>> Thanks all, in advance!
>>>
>>> --
>>> 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