generated fromtc39/template-for-proposals
- Notifications
You must be signed in to change notification settings - Fork6
Extensions proposal for ECMAScript
License
NotificationsYou must be signed in to change notification settings
tc39/proposal-extensions
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
// 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=$
// 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=$
- keep
obj::foo()
syntax for extension methods - repurpose
obj::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
- add
obj::ext:name
syntax - change operator precedence to same as
.
- remove
::obj.foo
(use cases can be solved by custom extension + library, or other proposals)
About
Extensions proposal for ECMAScript
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.