This is much rather a Javascript question than a Node.JS question. No,
there is no standard that you must do, and thats the great thing about
Javascript in that it provides you prototypes as a powerful basic
mechanism you can build code organisations with rather than forcing a
specific style of organisation on you like Java or C++.
If you cut away this hack
> if (!(this instanceof Tester)) {
> return new Tester(name);
> }
than what you did is the basic way prototypes work.
Otherwise you must precise your question. I for one came up with
following helper for (multiple) inheritance and use _ to prefix what I
consider a "private" variable. I don't know how well it works for
border cases, for me it does what I expect it to:
/**
| Subclassing helper.
|
| sub: prototype to become a subclass.
| base: either a prototype to become the base.
| or a table of prototypes to become the base for multiple
| inheritance.
*/
var subclass = function(sub, base) {
function Inherit() {}
if (base.constructor === Object) {
// multiple inheritance
for(var name in base) {
for(var k in base[name].prototype) {
if (k === 'constructor') continue;
if(Inherit.prototype[k]) {
throw new Error('Multiple inheritance clash for
'+sub+' :'+k);
}
Inherit.prototype[k] = base[name].prototype[k];
}
}
} else {
// single inheritance
Inherit.prototype = base.prototype;
}
sub.prototype = new Inherit();
sub.prototype.constructor = sub;
};
I like it, but it is likely not for everybody.
On Tue, Apr 17, 2012 at 1:53 PM, 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