How about a "named true" and a "named true property"

Gray Zhang otakustay at
Thu May 26 09:02:21 UTC 2016

I don't really know how to name this feature, but I find it quite neccessary

In JavaScript land there are many functions with parameters of type
boolean, and invocations to these functions do not have high readability,
for example jQuery have a `clone` method with a parameter deep of type
boolean, but when read a code like:

$.clone(sourceObject, true);

it's hard to understand what is that `true` stands for, a more unfamiliar
API could make this more uncomfortable:

control.dispose(true); // does it mean "to remove DOM node or anything

In practice we can write some comment after a boolean parameter to explain
what it is:

control.dispose(true /* removeNode */);

but it's a lot of work.

In this case I think we may introduce a handy syntax called "named true"
which only transforms to the keyword `true` but have a custom name:


Here a leading colon hints it is a "named true" so in parsing stage
`:removeNode` simply becomes `true`, and we can have a false value with
simply `!:removeNode`, it doesn't hit performance but notably increase

Boolean parameters is not good practice but I believe it is
not eliminated nowadays and will never be eliminated, so a more friendly
syntax could save a lot of time


Furthermore, there are also many functions accepting an `options` parameter
including some "flag properties", flag properties are simple properties
with a boolean value, a good example is the {silent: true} flag in many
event related APIs.

Constructing an `options` object containing flag properties are not easy,
we may repeat `: true` and `: false` many times, so with previously "named
true", I think a "named true property" could help to save time:

model.set('foo', newValue, {:silent, :disableValidation, !:destroyOldValue})

Here `:silent` is simply parsed to `silent: true` and `!:destroyOldValue`
is for `destroyOldValue: false`, it can save a large amount of code

All syntax here are just examples, not any propose to grammar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list