If you want a class to use that behavior, you can also just do this in
your javascript:
var Foo = function () {
if (!(this instanceof arguments.callee)) return new arguments.callee
();
// set up class...
}
Several built in classes behave differently depending on whether
they're called with new. Consider String.
o = {};
s = String(o);
assert(s === o.toString()); // pass
so = new String(o);
assert(so !== s); // pass - so is a String object.
assert( !(s instanceof String) ); // s is a native string, not a
String object.
assert( so instanceof String ); // so is a string object.
Of course, the natives in JS are a little "odd" compared with regular
objects. But, if you're defining a class from C++, it's not terribly
out of line to have it work differently when it's called as a regular
function.
--i
On Mar 30, 1:38 am, Stephan Beal <[email protected]> wrote:
> On Mon, Mar 30, 2009 at 8:15 AM, Ondrej Zara <[email protected]> wrote:
> > - "new" ignores return value of ctor function;
> > - "new" sets the hidden _proto_ property so prototype inheritance works;
> > - "new" call uses "this" keyword as a reference to new instance being
> > created, non-new call uses current scope (global or any other if ".call"ed
> > or ".apply"ed).
>
> Wow, that's a lot of differences. i can see the benefit of this
> distinction in JS-side constructors, but for bound types i'm still
> having trouble finding a use for the distinction. Nonetheless, since
> there is such a difference i guess i'll have to add a flag for class
> binders to toggle it with.
>
> Thanks for the clarification! :)
>
> --
> ----- stephan bealhttp://wanderinghorse.net/home/stephan/
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---