The question still stands, should we actually do this (.bind) or not, since
most
of the core API don't...
On 2011/03/22 16:04:17, Nebojša Ćirić wrote:
Also, defineProperty is part of Harmony but it seems only Opera doesn't
have
it
(FF4, IE9, Chrome 5 have it, and IE8 only for DOM).
On 2011/03/22 16:02:58, Nebojša Ćirić wrote:
> I've presented the problem to EcmaScript body, and they proposed 2
solutions.
>
> Q:
>
> So if we have something like this:
>
> LocaleInfo.Collator.prototype.compare = function(a, b) {
> ...
> };
>
> How do you specify it's bound by default?
>
> A:
>
> Two choices:
>
> 1) Don't put it on the prototype. Rather, make it part of each instance
state,
> don't use "this" in the function, and have the function lexically
capture
all
> the state it needs.
>
> 2) Put it on the prototype using a binding accessor:
>
> function makeBindingAccessorDescriptor(method) {
> return {
> get: function() { return method.bind(this); }
> set: writable: ??, enumerable: ??, configurable: ?? //
according to
your
> intensions
> };
> }
>
> Object.defineProperty(LocaleInfo.Collator.prototype, 'compare',
>
makeBindingAccessorDescriptor(function(a,
b)
{
> ....
> }));
>
> More details about definePrototype and get/set statements -
> http://ejohn.org/blog/ecmascript-5-objects-and-properties/
>
> Now the question is, can we do 2) in v8...
>
> On 2011/03/15 07:55:48, Mads Ager wrote:
> > On 2011/03/14 23:23:01, Jungshik Shin wrote:
> > > Thank you for the explanation.
> > >
> > > Is there any way to make 'array.sort(coll.compare)' work on my side
(in
C++)
> > or
> > > is that the way sort() is supposed to work no matter what? It seems
that
the
> > > latter is the case, but I'd better ask you to make sure.
> >
> > This is just the way JavaScript works. You are extracting the
function and
> > calling it with a receiver that is not a collator. You are right that
you
> cannot
> > make this work this way. I don't think you should in any case because
this
is
> > not how JS works.
> >
> > > I guess many callers want to define their own compare function any
way
> because
> > > they're likely to compare more complex objects than simple strings.
For
> > > instance, a file browser may want to list directories before regular
files
> and
> > > would use the following:
> > >
> > > var compareFileName = function(a, b) {
> > > if (a.type == b.type) { return coll.compare(a.name, b.name);}
> > > if (a.type =="directory") return -1;
> > > return 1;
> > > }
> > >
> > > So, not being able to directly pass coll.compare() is probably ok.
However,
> if
> > > there's any way to make it possible, pls let me know.
http://codereview.chromium.org/6673011/
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev