domenic at domenicdenicola.com (2013-11-12T18:53:32.527Z)
On Fri, Nov 1, 2013 at 12:16 PM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: > The option you give us is: keep abusing `Object.prototype.toString` ... > that's OK but it seems to be a dirty solution. In ES5, Object.prototype.toString was a reliable brand check. It no longer is in ES6.
On Fri, Nov 1, 2013 at 12:16 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote: > Allen I think we all agree duck typing is more flexible, powerful, easy, > etc etc ... but since you mentioned, how would do distinguish between a > `Map` and a `WeakMap` without passing through the branding check ? > > That's a very good example indeed, `'has' in obj` or `'set' in obj` does > not grant you any behavior with the signature itself (e.g. `WekMap` > does/did not accept primitives as key) > > AFAIK WebIDL is entirely based on branding, regardless it's a good or a > bad thing, that's the way W3C describes "things" since quite ever (and > implement them in Java too ... anyway) > > Why there's no easy way to check for branding if not abusing > `Object.prototype.toString` ? > > If so many libraries are using it because of the cross realm, maybe there > is a need for some similar mechanism and implemented in core would make > everybody life easier/faster ? > > The option you give us is: keep abusing `Object.prototype.toString` ... > that's OK but it seems to be a dirty solution. > In ES5, Object.prototype.toString was a reliable brand check. It no longer is in ES6. > > Cheers > > > > > On Fri, Nov 1, 2013 at 11:52 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote: > >> >> On Nov 1, 2013, at 10:40 AM, Anne van Kesteren wrote: >> >> > On Fri, Nov 1, 2013 at 12:50 AM, Allen Wirfs-Brock >> > <allen at wirfs-brock.com> wrote: >> >> [isSpecialString]() {return isSpecialString in this}; >> > >> > So this is basically what we have for DOM objects right now. And what >> > we're asking for is having this elevated to language-supported >> > construct. Be it in the form of a method or something else. It's not >> > really clear why sometimes you would use if "brand in obj" and other >> > times "Array.isArray(obj)". Seems rather messy. >> >> Array.isArray is a legacy artifact so I'm not going to try to generalize >> from it. >> >> But, in general, the distinction is between asking "does this object >> appear to expose the behavior I need" and "is this object an instance of a >> specific known implementation of the behavior I need". Let's call the >> first question "duck typing" and the second "branding". Duck typing is a >> weaker test but allows for a lot of flexibility and extensibility. Branding >> gives you reliable knowledge about certain aspects of a branded object. >> But it is less flexible and and systems that extensively using branding >> are often harder to extend. IMO branding is over used. >> >> Array.isArray is a specific brand test based upon how the implementation >> of how property definition interacts with the value of the 'length' >> property. >> >> ES currently proves lots of ways to do duck type testing. Built-in >> objects (including host objects) can implement branding and brand testing >> using various techniques but ES currently (including ES6) is very limited >> in its language level sort for reliable branding. The best we have to >> offer in ES6 is WeakMaps. In the future, we probably will add mechanism >> that make it easier to define branded objects defined via ES code. >> >> But, before you brand something really think carefully about why you need >> such a brand. It may not really be necessary. >> >> Allen > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -- Cheers, --MarkM -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131101/3976e7d3/attachment.html>