Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function Bind Syntax-friendly library API #65

Closed
UltCombo opened this issue May 7, 2015 · 8 comments
Closed

Function Bind Syntax-friendly library API #65

UltCombo opened this issue May 7, 2015 · 8 comments
Labels

Comments

@UltCombo
Copy link

UltCombo commented May 7, 2015

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.

@zloirock
Copy link
Owner

zloirock commented May 7, 2015

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.

@UltCombo
Copy link
Author

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.

@zloirock
Copy link
Owner

@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.

@UltCombo
Copy link
Author

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);

@zloirock
Copy link
Owner

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.

@UltCombo
Copy link
Author

Awesome, thanks!

@zloirock
Copy link
Owner

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

@UltCombo
Copy link
Author

Nice. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants