Functions - Arguments Object

46 views
Skip to first unread message

Si Robertson

unread,
Mar 16, 2015, 10:55:01 PM3/16/15
to streng...@googlegroups.com
From the proposal ...

  It is a (early) ReferenceError to refer to the implicit 'arguments' variable
  inside a 'function' expression or declaration.

How do we create proxy functions to change the execution context of event listeners etc in strong mode?

  function proxy(context, func) {
    let n = func.length;

    if (n === 0) {
      return function() {
        return func.call(context);
      }
    }

    if (n === 1) {
      return function(a) {
        return func.call(context, a);
      }
    }

    ...

    return function() {
      return func.apply(context, arguments); // error?
    }
  }

I'm aware of Object.bind() but custom functions are typically optimal and obviously customizable.

Sébastien Doeraene

unread,
Mar 17, 2015, 1:59:14 AM3/17/15
to Si Robertson, streng...@googlegroups.com

Hi,

You can use a rest parameter for this purpose:

return function(...args) {
  return func.apply(context, args);
}

In general, rest parameters supersede usages of arguments when it comes to dealing with varargs. Even in regular ES6.

Also, consider using arrow functions in the first place instead, which keep the `this` from the enclosing method/function.

Cheers,
Sébastien

--
You received this message because you are subscribed to the Google Groups "Strengthen JS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strengthen-j...@googlegroups.com.
To post to this group, send email to streng...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/strengthen-js/ad3a751f-bd61-4c76-94d9-d2124d2cd631%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Si Robertson

unread,
Mar 17, 2015, 4:58:51 AM3/17/15
to Sébastien Doeraene, streng...@googlegroups.com
Thanks, Sébastien

Using the rest parameter makes sense, hopefully V8 will support that feature soon.

Sébastien Doeraene

unread,
Mar 17, 2015, 5:04:44 AM3/17/15
to Si Robertson, streng...@googlegroups.com
Hi,

V8 already supports rest parameters, but you need to enable them with --harmony-rest-parameters. At least that works for me with a d8 I built around a week ago.

Sébastien

Dmitry Lomov

unread,
Mar 17, 2015, 5:49:12 AM3/17/15
to Sébastien Doeraene, Si Robertson, streng...@googlegroups.com
On Tue, Mar 17, 2015 at 10:04 AM, Sébastien Doeraene <sjrdo...@gmail.com> wrote:
Hi,

V8 already supports rest parameters, but you need to enable them with --harmony-rest-parameters. At least that works for me with a d8 I built around a week ago.

Just to clarify, rest parameters are work-in-progress for now (although we actively working on them) - do not use in production :)
Reply all
Reply to author
Forward
0 new messages