zloirock / core-js

Standard Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Function Bind Syntax-friendly library API

UltCombo opened this issue · comments

Would it be possible to expose a library API that works with the Function Bind Syntax?

I mean, an API similar to the current library API, but which uses the this binding instead of the first argument for instance methods. For instance, instead of:

var core = require('core-js/library');
core.String.repeat('*', 10);

Do something like:

var core = require('core-js/library');

'*'::core.String.prototype.repeat(10);
// transpiles to:
core.String.prototype.repeat.call('*', 10);

Needless to say, this would be an excellent way to avoid polluting the built-in prototypes while maintaining composability.

Now it's missing. Before, I wanted to add it for abstract references proposal and added it for Dict module, see example from this comment, but with current proposal it's impossible. I'll try add something like this when will be available this syntax in compilers, but it will not be so convenient as with abstract refs.

ES7 function bind has been implemented in Babel -- babel/babel#1518 and babel/babel@724bf52

Any chance to have bind-operator-friendly methods soon? I'll be giving a talk to ~300 people this Saturday and it would be nice to demonstrate core-js + bind operator. 😃

If you give me the directions on how this should be approached, I can help implementing it too.

@UltCombo I can add it as you proposed - '*'::core.String.prototype.repeat(10), but not sure it's a convenient API. Maybe as temporary solution.

Yes, I believe it can be improved, but I can't think of a better solution at the moment.
Using Babel, the syntax is not too ugly though:

import { String as String_ } from 'core-js';
const { repeat, startsWith /*...*/ } = String_.prototype;

'*'::repeat(10);

Should be available in 0.9.9.

Problem with single entry points for methods - currently only for static versions. I'll think about it later.

If someone have better ideas about this API - awaiting proposals.

Awesome, thanks!

In 2.0 in library version it's changed to core.%CONSTRUCTOR%.virtual.%METHOD% and available special entry points.

Nice. 👍