On Apr 5, 9:15 am, Rob Brackett <[email protected]> wrote:
> This seems like it could be a pretty valuable tool, but what about objects
> that implement valueOf() specifically so arithmetic can be performed on them
> (such as Date)?

Thanks for your feedback. On purpose, restrict mode doesn't allow
applying the minus operator to anything else but primitive numbers,
including Date objects - so you need to apply date.valueOf() or
Number(date) first, as you point out.

It's quite interesting that you chose Date as an example since that
object happens to be hard-coded into the JS operator semantics like
nothing else. Check it out for yourself
https://github.com/olov/jsshaper/blob/master/src/ecma-ops/ecma-ops.js
(__defaultValue) or ECMA-262 3rd ed 8.6.2.6.

Because of this - is subtraction but + is string concatenation, when
applied to Date objects:
js> date1-date2
345600000
js> date1+date2
"Sat Mar 05 2011 00:00:00 GMT+0100 (CET)Tue Mar 01 2011 00:00:00 GMT
+0100 (CET)"

It's impossible to mimic these semantics with a standard object. Try
it out with two objects that return numbers in valueOf and strings in
toString.

And that's why I think it's not a bad thing that restrict mode
requires you to write date1.valueOf() - date2.valueOf(), or
Number(date1) - Number(date2) depending on which you prefer.

Now there's a general case too of course, for example object Numbers
and Strings. I haven't found found much code for which I would
consider loose operator semantics something that's really useful in
practice.

If your program uses boxed Numbers, Strings or Booleans in a way that
really clashes with restrict mode, more so than requiring occasional
explicit primitive conversions, I'd like to hear more about it.
Similarly so for objects implementing custom valueOf-methods
(returning numbers), then being used for number arithmetics. I don't
think it is a big issue, but I could be wrong. Please let me know.

Since my original post, I've created a site for Shaper <http://
jsshaper.org/> and posted a few Shaper and restrict mode related
articles on my blog <http://blog.lassus.se/>. I have also started to
investigate how well the JS subset defined by restrict mode matches
real well-written JS programs. I've written about v8bench (great
match) and will soon post my findings about kraken and jQuery.

Again, thank you for your feedback.


/Olov

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to