Re: an idea for replacing arguments.length

2013-11-11 Thread Corey Frang
I hate to bring this up, as I'm sure I've missed a bunch of the arguments
that define WHY, but if this is the case, it seems unintuitive to me that
passing undefined still results in a default param being set.


function test(a = 1) { console.log(a); }

test(); // gets 1 - yay
test(undefined); // gets 1 - boo! Expect: undefined

(sorry tab, forgot to reply all on the first reply :( )


On Mon, Nov 11, 2013 at 12:37 PM, Tab Atkins Jr. jackalm...@gmail.comwrote:

 On Mon, Nov 11, 2013 at 9:21 AM, Allen Wirfs-Brock
 al...@wirfs-brock.com wrote:
  On Nov 11, 2013, at 6:10 AM, Mark S. Miller wrote:
  On Sun, Nov 10, 2013 at 11:33 PM, Corey Frang gnar...@gmail.com
 wrote:
  Just to provide another way of working around it:
 
  var empty = {}; // or even a symbol?
 
  function splice(start = empty, count = 0, ...items) {
if (start === empty) { ...
  }
 
  That is excellent! Has all the upsides of the best of the other
 proposals
  and none of the downsides.
 
  No, this doesn't work because then both
   [1,2,3].splice()
   [1.2.3].splice(undefined)
 
  will both result in start === empty.  But for web compatibility they
 must be
  treated differently.

 Right.  The one and only way to test for missing args different from
 undefined is to use a rest arg and test length, as demonstrated
 earlier in this thread.

 ~TJ

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: an idea for replacing arguments.length

2013-11-11 Thread Corey Frang
Sure, it makes some sense when you see it like that :)

I retract my this doesn't make sense

I guess we are stuck with (...args)

On Mon, Nov 11, 2013 at 2:37 PM, Brandon Benvie bben...@mozilla.com wrote:

  On 11/11/2013 11:31 AM, Corey Frang wrote:

 I hate to bring this up, as I'm sure I've missed a bunch of the arguments
 that define WHY, but if this is the case, it seems unintuitive to me that
 passing undefined still results in a default param being set.


  function test(a = 1) { console.log(a); }

  test(); // gets 1 - yay
 test(undefined); // gets 1 - boo! Expect: undefined

  (sorry tab, forgot to reply all on the first reply :( )


 The reason for this is so that you can do:

 ```js
 function foo(bar) {
   return delegatedFoo(foo, bar);
 }

 function delegatedFoo(foo, bar = bar) {
   return foo + bar;
 }

 foo(); // foobar and not fooundefined
 ```

 This kind of argument-passing delegation is a very common in JS.

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: an idea for replacing arguments.length

2013-11-10 Thread Corey Frang
Just to provide another way of working around it:

var empty = {}; // or even a symbol?

function splice(start = empty, count = 0, ...items) {
  if (start === empty) { ...
}


On Mon, Nov 11, 2013 at 1:22 AM, Jonas Sicking jo...@sicking.cc wrote:

 On Mon, Nov 11, 2013 at 2:12 AM, Allen Wirfs-Brock
 al...@wirfs-brock.com wrote:
  One of the the few remaining uses of a function's 'arguments' binding is
 to
  determine the actual number of passed arguments.  This is necessary in
 some
  overloading scenarios where a function has different behavior when an
  argument is completely absent then it has when undefined (or any other
  default value) is explicitly passed in that parameter position.  That
  situation occurs in a number of DOM APIs and even a few ES library
  functions.

 I think the number is very low. I think I've heard of a total of four
 DOM functions which currently treat not passed as different from
 explicitly passed undefined. And we're working on changing those so
 that the two are treated the same. Unclear if we'll be able to change
 all of them.

 So from the DOM point of view, I don't see a lot of reason to add
 convenience capabilities for detecting that difference.

 And in none of the cases in the DOM, does function.length become a
 problem. This is because in all of the cases the argument truly is
 optional, which means that .length does not include it. So for example
 for XMLHttpRequest.open you could write a JS implementation that looks
 like:

 function open(verb, url, ...args) {
   let [async, username, passwd] = args;
   if (args.length == 0) {
 async = true;
   }
   ... more code here ...
 }

 It is surprising to me that that is not the case in the ECMAScript
 APIs, but seems like that is history.

 / Jonas
 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss