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

As currently we do this for variable. Like this

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

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

example 2

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

Current

const name = 'name with space'

const obj = {name}

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

With this proposal


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

1 Like

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

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

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

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 keyso 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 thekey` and that would work fine as well cause JS allows that so it will

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

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.

1 Like

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

cc @ljharb any thoughts on this ?

What about calling a function with flag?

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.

post withdrawn by author,

can you explain what's happening?

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