# when object declaration passed with single string, consider the same string as key == value like variable shorthand

**URL:** <https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [March 3, 2020, 10:39am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249 "2020-03-03T10:39:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anikethsaha](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/anikethsaha/32/176_2.png) [@anikethsaha](https://es.discourse.group/u/anikethsaha)\
**Post date:** [March 3, 2020, 10:39am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/1 "2020-03-03T10:39:51Z")

</div>

As currently we do this for variable. Like this

```javascript
const name = 'jo'
const obj = {name}
console.log(obj) // output => {name : 'jo'}

```

### Proposal

So have the same behavior with `string` _type_ as well.

like this

```javascript
const obj = {'jo'}
console.log(obj) // output => { jo : 'jo' }

```

**example 2**

with **spaces** , it would be work like usual string with space behavior

**Current**

```javascript
const name = 'name with space'

const obj = {name}

console.log(obj) 
// output => {'name-with-space': 'name with space'}

```

**With this proposal**

```javascript

const obj = {'value with space'}

console.log(obj) 
// output => {'value-with-space' : 'value with space' }

```

### Pros

As we can see from this examples, we need a separate variable to do this shorthand  
_(ofcourse we can avoid using this separate variable but then we cant do this shorthand)_ with this proposal, we dont need that extra variable for doing this shorthand.

### Cons

May be some conflict with rare cases. I didnt find any as of now. Please do let me know if you think

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [March 3, 2020, 9:17pm UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/2 "2020-03-03T21:17:03Z")

</div>

I don't think this would conflict with any other syntax, but I doubt there are enough common use cases for this.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 3, 2020, 10:33pm UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/3 "2020-03-03T22:33:15Z")

</div>

Did you mean the final output to be `// output => {'value with space' : 'value with space' }` (ie, spaces in the key instead of dashes)?

---

<div class="post-metadata">

**Author:** ![anikethsaha](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/anikethsaha/32/176_2.png) [@anikethsaha](https://es.discourse.group/u/anikethsaha)\
**Post date:** [March 4, 2020, 5:06am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/4 "2020-03-04T05:06:59Z")

</div>

when I mean `value with space`. The `key` has to be filled with dashes as I think keys having spaces in it might have limited access approach like

if its

```javascript
const a = {
'value with space' : 'value with space'
}

```

it is possible, fare enough !  
but we can access it only using this `a[`value with space`]` not using this `a.value with space`

ALso, I made a mistake in the post with adding `-` instead of `underscrore (`\_`) in the `key`so I think it should be`{ value\_with\_space : 'value with space' }`. this is just for safe side. We can surely omit any kind special char and keep the space as it is even for the`key` and that would work fine as well cause JS allows that so it will

---

<div class="post-metadata">

**Author:** ![anikethsaha](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/anikethsaha/32/176_2.png) [@anikethsaha](https://es.discourse.group/u/anikethsaha)\
**Post date:** [March 4, 2020, 5:08am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/5 "2020-03-04T05:08:23Z")

</div>

I think it has use-cases but currently we are able to tackle those using temporary variables or hard code the object without shorthand.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [March 4, 2020, 5:42am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/6 "2020-03-04T05:42:39Z")

</div>

I think it would be a very bad idea to automatically change spaces to another character - it'd be confusing, and hard to maintain and debug.

---

<div class="post-metadata">

**Author:** ![anikethsaha](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/anikethsaha/32/176_2.png) [@anikethsaha](https://es.discourse.group/u/anikethsaha)\
**Post date:** [March 4, 2020, 6:55am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/7 "2020-03-04T06:55:41Z")

</div>

yeah fair enough ! Even keeping it as it works fine !

---

<div class="post-metadata">

**Author:** ![anikethsaha](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/anikethsaha/32/176_2.png) [@anikethsaha](https://es.discourse.group/u/anikethsaha)\
**Post date:** [March 5, 2020, 3:17pm UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/8 "2020-03-05T15:17:10Z")

</div>

cc @ljharb any thoughts on this ?

---

<div class="post-metadata">

**Author:** ![mnini](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mnini/32/253_2.png) [@mnini](https://es.discourse.group/u/mnini)\
**Post date:** [March 7, 2020, 4:47am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/10 "2020-03-07T04:47:06Z")

</div>

What about calling a function with flag?

---

<div class="post-metadata">

**Author:** ![mnini](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mnini/32/253_2.png) [@mnini](https://es.discourse.group/u/mnini)\
**Post date:** [March 7, 2020, 4:51am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/11 "2020-03-07T04:51:30Z")

</div>

I think, Object.prototype.constructor should accept stuff that's inserted into it even with {name} possibly typeof name = 'mentioned'.

With this type and accessed constructor, everybody can do what does he wants.

Namespace to avoid protytype polution...

var object = \lib{

}...

Therefore you can do whatever, and other programmers in their libraries too.

Maybe even as argument for function there could be function(target, overwrite)  
where overwrite would be a named parameter...

This solution covers most use cases to all degree of freedom while being able to run in same VM we can achieve.

---

<div class="post-metadata">

**Author:** ![anikethsaha](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/anikethsaha/32/176_2.png) [@anikethsaha](https://es.discourse.group/u/anikethsaha)\
**Post date:** [March 7, 2020, 6:14am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/12 "2020-03-07T06:14:26Z")

</div>

> post withdrawn by author,

can you explain what's happening?

---

<div class="post-metadata">

**Author:** ![mnini](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/mnini/32/253_2.png) [@mnini](https://es.discourse.group/u/mnini)\
**Post date:** [March 7, 2020, 8:13am UTC](https://es.discourse.group/t/when-object-declaration-passed-with-single-string-consider-the-same-string-as-key-value-like-variable-shorthand/249/13 "2020-03-07T08:13:39Z")

</div>

I had a typo... Reflex is to delete and rewrite, not to edit, sorry. Problem was that I haven't read whole idea.
