self functions

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

self functions

Bucaran
Add a `self` decorator to functions that makes them return `this` by default.

                export function self myMethod () {
                        // return this by default
                }

This decorator could be used to make any function bound to the current scope `this` as well so:

                func(function self () { // this is bound to my dad’s scope })

Would be roughly equivalent to:

                func(() => { // this is bound to my dad’s scope })

Although I personally would favor the arrow function syntax, the `self` decorator could be
used to optimize binding generators, so instead of:

                func(function* () { }.bind(this))

One could write:

                func(function self* () { })

Similary it could be used in promise handlers, so instead of:

                new Promise(function (resolve, reject) { }.bind(this))

One could write:

                new Promise(function self (resolve, reject) { })

It would be even sweeter if you didn’t need to specify the keyword function when writing `self` functions:

                new Promise(self (resolve, reject) { })
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: self functions

Frankie Bagnardi
Aside from returning `this` you're suggesting an alternate syntax for arrow functions? I don't see any value here.

More importantly, the syntax is ambiguous for unnamed functions.

```js
// already valid syntax
var self = function self(){};
```



On Mon, Jul 27, 2015 at 7:06 PM, Bucaran <[hidden email]> wrote:
Add a `self` decorator to functions that makes them return `this` by default.

                export function self myMethod () {
                        // return this by default
                }

This decorator could be used to make any function bound to the current scope `this` as well so:

                func(function self () { // this is bound to my dad’s scope })

Would be roughly equivalent to:

                func(() => { // this is bound to my dad’s scope })

Although I personally would favor the arrow function syntax, the `self` decorator could be
used to optimize binding generators, so instead of:

                func(function* () { }.bind(this))

One could write:

                func(function self* () { })

Similary it could be used in promise handlers, so instead of:

                new Promise(function (resolve, reject) { }.bind(this))

One could write:

                new Promise(function self (resolve, reject) { })

It would be even sweeter if you didn’t need to specify the keyword function when writing `self` functions:

                new Promise(self (resolve, reject) { })
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: self functions

Jussi Kalliokoski
In reply to this post by Bucaran
For classes, you can use decorators [1][1], if they eventually get in in the language (just one extra symbol):

```JS
function self (object, name, descriptor) {
  var original = descriptor.value;
  return {
    ...descriptor,
    value (...args) {
      original.apply(this, args);
      return this;
    },
  };
}

class Foo {
  constructor () {
    this.x = 0;
  }
 
  @self
  method () {
    this.x++;
  }
}

console.log(new Foo().method().x) // 1
```

for ES5 style "classes", you can just use functions (2 extra symbols):

```JS
function self (original) {
  return function (...args) {
    original.apply(this, args);
    return this;
  };
}

function Foo () {
  this.x = 0;
}

Foo.prototype.method = self(function () {
  this.x++;
});

console.log(new Foo().method().x) // 1
```

On Tue, Jul 28, 2015 at 5:06 AM, Bucaran <[hidden email]> wrote:
Add a `self` decorator to functions that makes them return `this` by default.

                export function self myMethod () {
                        // return this by default
                }

This decorator could be used to make any function bound to the current scope `this` as well so:

                func(function self () { // this is bound to my dad’s scope })

Would be roughly equivalent to:

                func(() => { // this is bound to my dad’s scope })

Although I personally would favor the arrow function syntax, the `self` decorator could be
used to optimize binding generators, so instead of:

                func(function* () { }.bind(this))

One could write:

                func(function self* () { })

Similary it could be used in promise handlers, so instead of:

                new Promise(function (resolve, reject) { }.bind(this))

One could write:

                new Promise(function self (resolve, reject) { })

It would be even sweeter if you didn’t need to specify the keyword function when writing `self` functions:

                new Promise(self (resolve, reject) { })
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: self functions

Isiah Meadows
In reply to this post by Bucaran

+1 for the decorator/function solution. It hardly affects performance in practice, since the resulting wrapped function is a whopping two lines by itself, and engines can optimize that very easily and efficiently. I don't think there needs to be new syntax for it.

One thing I like about JavaScript: its flexibility. I'd say that after ES7 or ES8, there won't be a ton of reason for syntactic additions.


From: Jussi Kalliokoski <[hidden email]>
To: Bucaran <[hidden email]>
Cc: "[hidden email] >> es-discuss" <[hidden email]>
Date: Tue, 28 Jul 2015 09:43:58 +0300
Subject: Re: self functions

For classes, you can use decorators [1][1], if they eventually get in in the language (just one extra symbol):

```JS
function self (object, name, descriptor) {
  var original = descriptor.value;
  return {
    ...descriptor,
    value (...args) {
      original.apply(this, args);
      return this;
    },
  };
}

class Foo {
  constructor () {
    this.x = 0;
  }
 
  @self
  method () {
    this.x++;
  }
}

console.log(new Foo().method().x) // 1
```

for ES5 style "classes", you can just use functions (2 extra symbols):

```JS
function self (original) {
  return function (...args) {
    original.apply(this, args);
    return this;
  };
}

function Foo () {
  this.x = 0;
}

Foo.prototype.method = self(function () {
  this.x++;
});

console.log(new Foo().method().x) // 1
```

[1]: https://github.com/wycats/javascript-decorators

On Tue, Jul 28, 2015 at 5:06 AM, Bucaran <[hidden email]> wrote:

Add a `self` decorator to functions that makes them return `this` by default.

                export function self myMethod () {
                        // return this by default
                }

This decorator could be used to make any function bound to the current scope `this` as well so:

                func(function self () { // this is bound to my dad’s scope })

Would be roughly equivalent to:

                func(() => { // this is bound to my dad’s scope })

Although I personally would favor the arrow function syntax, the `self` decorator could be
used to optimize binding generators, so instead of:

                func(function* () { }.bind(this))

One could write:

                func(function self* () { })

Similary it could be used in promise handlers, so instead of:

                new Promise(function (resolve, reject) { }.bind(this))

One could write:

                new Promise(function self (resolve, reject) { })

It would be even sweeter if you didn’t need to specify the keyword function when writing `self` functions:

                new Promise(self (resolve, reject) { })
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss