A way to prevent properties to be added to an object if they are null or undefined.

Rodrigo Carranza rodrigocarranza at outlook.com
Wed Nov 29 02:58:22 UTC 2017


Yes, like that, but that will require to modify the object.

Compare this

```js
let a = {}
a.field ?= couldBeNull
```

To:

```js
let a = {
  field ?: couldBeNull
}
```

Also if you want to set the property with the same name of the variable
```js
let field = ...
let a = {
  field?
}
```

Why this is useful?
Well, is not the biggest change but it could make the language more expressive.
Also this is similar to how Typescript declares optional properties in object interfaces.

```ts
interface User{
  id: string,
  username: string,
  age?: number,
}
```

then:

```js
function createUser({id, username, age}){
  db.user.insertOne({id, username, age?})
}



On nov. 28 2017, at 8:45 pm, Sebastian Malton <sebastian at malton.name> wrote:
So something like the following?


```js
a.field ?= value;
```

And this sets the property `field` of object `a` to `value` if `value != null`.


Sebastian Malton

From: rodrigocarranza at outlook.com
Sent: November 28, 2017 8:40 PM
To: sebastian at malton.name
Cc: es-discuss at mozilla.org
Subject: Re: A way to prevent properties to be added to an object if they are null or undefined.


It's about the value of the property to be added, nothing to do with the name of the property. It should discard properties whose value are null(but this behavior is not always desired, so a different syntax for these properties should be added).

```

On nov. 28 2017, at 8:33 pm, Sebastian Malton <sebastian at malton.name<mailto:sebastian at malton.name>> wrote:
I am sort of confused about what you are asking, is the check about the property name or the current value of the property?


Sebastian Malton

From: rodrigocarranza at outlook.com<mailto:rodrigocarranza at outlook.com>
Sent: November 28, 2017 8:30 PM
To: es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>
Subject: A way to prevent properties to be added to an object if they are null or undefined.


A way to prevent properties to be added to an object if they are null or undefined.

Currently this can be accomplished in many ways:

With an if:
```js
function foo(couldBeNull){
let ret = {}
if(couldBeNull){
ret.couldBeNull<http://ret.couldbenull/?recipient=sebastian%40malton.name> = couldBeNull
}
return  ret
}
```

With ternary (kind of gross)
```js
function foo(couldBeNull){
let ret = {}
couldBeNull ? (ret.couldBeNull<http://ret.couldbenull/?recipient=sebastian%40malton.name> = couldBeNull) : null
return  ret
}
```

Also gross
```js
function foo(couldBeNull){
let ret = {}
couldBeNull && (ret.couldBeNull<http://ret.couldbenull/?recipient=sebastian%40malton.name> = couldBeNull)
return  ret
}
```

A bit hard to read:
```js
function foo(couldBeNull){
let ret = {
...({couldBeNull} : {})
}
return  ret
}
```

Requires importing a lib or writing the function by yourself. Also it has to iterate over all values
```js
function foo(couldBeNull){
let ret = {
couldBeNull
}
ret = removeEmptyValues(ret) // imported from some library
return  ret
}
```

Wouldn't it be better something like this?:

```js
function foo(couldBeNull){
let ret = {
couldBeNull?
}
return  ret
}
```

Or if you want to set other property name

```js
function foo(couldBeNull){
let ret = {
bar ?:  couldBeNull // bar is not added if couldBeNull is null or undefined
}
return  ret
}
```

[https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif]<https://www.avast.com/sig-email?utm_medium=email&%3Butm_source=link&%3Butm_campaign=sig-email&%3Butm_content=emailclient&recipient=sebastian%40malton.name>   Libre de virus. www.avast.com<https://www.avast.com/sig-email?utm_medium=email&%3Butm_source=link&%3Butm_campaign=sig-email&%3Butm_content=emailclient&recipient=sebastian%40malton.name>
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>
https://mail.mozilla.org/listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss?recipient=sebastian%40malton.name>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171129/9673aa51/attachment-0001.html>


More information about the es-discuss mailing list