Code Monkey home page Code Monkey logo

proposal-method-extraction's Introduction

Method extraction for JavaScript

ECMAScript Stage-0 Proposal. J. S. Choi, 2021.

Why a method-extraction operator

Function.prototype.bind is very common in object-oriented JavaScript code. It is a useful method that allows us to extract object methods into bound functions that are not dependent on a particular this value and which may be used in any object environment. bind serves as an important link between object-oriented and functional APIs in JavaScript.

Why then would an operator that does the same thing? Because there is an important difference.

Function.prototype.bind is insecure

When we run our code in an untrusted environment, global objects may be modified. In particular, Function.prototype.call

JHD: Want to delete Function.prototype.call and things still work

JHD: Because then I'm not relying on the .call API. It's not super common to be robust against things like this, but that doesnt mean its not a good goal. We need to allow users to harden their code and prevent edge cases like this.

JHD: Defense model here is that you run code in an environment you trust, but after that anything could happen. I use Function.bind.call to protect against this.

Method extraction is common but clunky

???

No other current TC39 proposal addresses method extraction

There is a proposal for an extension-method operator :: that ostensibly addresses a similar problem. However, the extension-method proposal does not solve method extraction and explicitly calls it out as an orthogonal non-goal.

In addition, the extension-method proposal is a successor to an older proposal for a bind operator ::. This older proposal did address method extraction, but it is now inactive.

Description

(A formal specification is available.)

Method extraction &. is a left-associative infix operator that binds its right-hand side (a method identifier or a dynamic method name in [ ]) to its left-hand side (the method’s original object), creating a bound function in the same manner as Function.prototype.bind.

For example, arr&.slice would be roughly equivalent to arr.slice.bind(arr), except that its behavior does not change if code elsewhere reassigns the global method Function.prototype.bind.

Likewise, obj&.[Symbol.iterator] would be roughly equivalent to obj[Symbol.iterator].bind(obj).

Method extraction has equal precedence with member expressions, call expressions, new expressions with arguments, and optional chains.

Left-hand side Example
Primary expressions a&.m
Member expressions a.b&.m
Call expressions a()&.m
new expressions with arguments new F()&.m
Optional chains a?.b&.m

Similarly to the ?. optional-chaining token, the &. token may be padded by whitespace. For example, a &. m
is equivalent to a&.m,
and a &. [Symbol.iterator]
is equivalent to a&.[Symbol.iterator].

Real-world examples

Only minor formatting changes have been made to the status-quo examples.

Status quo With method extraction
???

From ???.

???

proposal-method-extraction's People

Contributors

domenic avatar jakearchibald avatar js-choi avatar probins avatar robpalme avatar styfle avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

proposal-method-extraction's Issues

Alternatives

Except a special binary notation for method extraction (obj&.method or obj.&method()), there are some alternatives:

partial func application proposal

obj.method(...) (syntax precedents: PHP!)

Note if we go with Hack style pipeline, we will not have partial func app anymore. But it's very possible to have +> obj.method(...) instead.

refs proposal:

see rbuckton/proposal-refs#9

The problem is, as current ref proposal we actually need code like setTimeout(do { let ref m = ref obj.method; m } which seems doesn't make sense...

Extensions proposal:

Though extensions do not support method extraction directly, it allow userland solution with nice syntax like obj::bindMethod:method, and it could also support extract accessors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.