Primitives vs. Objects, Ruby-like features

Darryl psygnisfive at yahoo.com
Mon Mar 24 19:43:28 PDT 2008


In current versions of JS there's some weird stuff
where some primitives are equal to their object
equivalents:

1 == new Number(1)

But in other cases they're not equivalents at all:

typeof(1) == "number"
typeof(new Number(1)) == "object"

And sometimes theres weird syntax errors:

5.prototype //parse error

function N(n){ return n }
N(5).prototype //== Number, wtf?

Will these be made uniform in JS2? I would think that
all numbers should be Numbers, with addressable
methods like any other literal object. This would be
very convenient, because then we could have Ruby-like
methods such as times.

It'd also be nice to have non-mandatory function call
parens like in Ruby, and to have blocks-as-objects
like in Ruby. Both of these things make Ruby
delightfully easy to read. Consider this in Ruby:

5.times { puts "Foo! " }

which outputs "Foo! Foo! Foo! Foo! Foo! "

to do similar in JS, you need either

for(var i = 0; i < 5; i++ ){
alert("Foo!")
}

or you need to something like this:

N(5).times(function(){ alert("Foo!") })

which, if we had the above suggestions in place, could
instead be

5.times { alert("Foo!") }

JS already has blocks as .. "things", theoretically,
but not addressably so. Surely having block objects
would be plausible.

I'm sure this can be achieved. Ruby manages with
fairly similar syntax as JS, in many regards. It even
has similar hash notation: { foo => bar, baz => quux }
instead of { foo: bar, baz: quuz }, so having block
objects wont conflict there. The only possibly
conflict I can imagine is between

foo(n){ ... } as the argless foo returning a function
that takes a block with block-arg n, and foo(n){ ... }
as a function taking a single arg n and returning a
function that takes a block as an argument. This, i
think, can be handled by doing arg-number checking on
foo: if it takes 0 args as defined, then it's foo(
(n){ ... } ), but if it takes one or more args then
its (foo(n))( { ... } ). Perhaps to avoid confusion,
Ruby-like blocks can have block args specific
differently, like ruby block args: { |x| ... } instead
of (x){ ... }.

Having other ruby-like features ( object/hash args not
requiring {} in the right contexts) would be very cool
too. It'd be nice to be able to do

foo bar: 5, baz: "a"

instead of

foo({ bar: 5, baz: "a"})

Obviously this would have to have similar restrictions
on where it can appear just like ruby's version, but
still.

I know, I'm a Ruby whore, but you gotta admit, ruby
has delightfully easy-to-read syntax and easy-to-read
is easy-to-code.

-------------------------------

o///
Be seeing you...


      ____________________________________________________________________________________
Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs



More information about the Es4-discuss mailing list