function Ctor(){
var self = Ctor.prototype.isPrototypeOf(this) ? this :
Object.create(Ctor.prototype);
/* do stuff to self */
return self;
}
*isPrototypeOf *is more useful than *instanceof *in a bunch of ways.
* Object.prototype.isPrototypeOf.call(something) will never throw.
* *isPrototypeOf c*an be used for Objects create meaning you can use it to
check if an object inherits directly from another with no Ctor involved.
* *instanceof *checks that* Ctor.prototype* is in the prototype chain at
time of call, same as *isPrototypeOf *checks *__proto__ *at time of call,
but it's way more likely that a function's *prototype *property will change
(and valid core Javascript vs. *__proto__ *being an extension that only
recently was standardizing as optional even discussed).
* *instanceof *can return false despite the relationship between the *Ctor *and
the instance being relevant information (the constructor logic that runs on
an object is often very important for its entire life). The relationship
between an object and its [[prototypes]] is only important while that
relationship still exists. If it changes for some reason then there's not
much reason to care anymore.
* It's possible to usefully inherit from builtins like Array that will
always fail *instanceof* checks but isPrototypeOf just works how you want
it to.
function SubArray(){}
SubArray.prototype = [];
var x = new SubArray;
Array.prototype.isPrototypeOf(x) === true;
x instanceof Array; //false
In the original example, using *Object.create* may be questionable there as
simply using *new *to make the instance is probably faster, but it can
sometimes be beneficial to not add another frame to the call stack.
--
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