Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Extensions proposal for ECMAScript

License

NotificationsYou must be signed in to change notification settings

tc39/proposal-extensions

Repository files navigation

Proposal status

This is an ECMAScript (JavaScript) proposal in stage 1.

Note: The proposal could be seen as the reshape of the "virtual method" part of oldbind operator proposal, seetc39/proposal-bind-operator#56.

Simple examples

Example of ad-hoc extension methods and accessors

// define two extension methodsconst ::toArray=function(){return[...this]}const ::toSet=function(){returnnewSet(this)}// define a extension accessorconst ::allDivs={get(){returnthis.querySelectorAll('div')}}// reuse built-in prototype methods and accessorsconst ::flatMap=Array.prototype.flatMapconst ::size=Object.getOwnPropertyDescriptor(Set.prototype,'size')// Use extension methods and accesors to calculate// the count of all classes of div element.letclassCount=document::allDivs::flatMap(e=>e.classList::toArray())::toSet()::size

roughly equals to:

// define two extension methodsconst$toArray=function(){return[...this]}const$toSet=function(){returnnewSet(this)}// define a extension accessorconst$allDivs={get(){returnthis.querySelectorAll('div')}}// reuse built-in prototype method and accessorconst$flatMap=Array.prototype.flatMapconst$size=Object.getOwnPropertyDescriptor(Set.prototype,'size')// Use extension methods and accesors to calculate// the count of all classes of div element.let$$=$allDivs.get.call(document)$=$flatMap.call($,e=>$toArray.call(e.classList))$=$toSet.call($)$=$size.get.call($)letclassCount=$

Example of using constructors or namespace object as extensions

// util.jsexportconsttoArray=iterable=>[...iterable]exportconsttoSet=iterable=>newSet(iterable)
import*asutilfrom'./util.js'const ::allDivs={get(){returnthis.querySelectorAll('div')}}letclassCount=document::allDivs::Array:flatMap(e=>e.classList::util:toArray())::util:toSet()::Set:size

roughly equals to:

import*asutilfrom'./util.js'const$allDivs={get(){returnthis.querySelectorAll('div')}}let$$=$allDivs.get.call(document)$=Array.prototype.flatMap.call($,e=>util.toArray(e.classList))$=util.toSet($)$=Object.getOwnPropertyDescriptor(Set.prototype,'size').get.call($)letclassCount=$

Changes of the old bind operator proposal

  • keepobj::foo() syntax for extension methods
  • repurposeobj::foo as extension getters and addobj::foo = as extension setters
  • separate namespace for ad-hoc extension methods and accessors, do not pollute normal binding names
  • addobj::ext:name syntax
  • change operator precedence to same as.
  • remove::obj.foo (use cases can be solved by custom extension + library, or other proposals)

Other matrials

About

Extensions proposal for ECMAScript

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp