简体   繁体   中英

TypeError: Cannot create property 'FOO' on string 'BAR'

A wrong (and now fixed) code in our app triggered this error :

TypeError: Cannot create property 'FOO' on string 'BAR'

But Javascript completely allows setting free properties on a string variable. I just tried it in Chrome console :

'BAR'.FOO = 'hello'
'BAR'['FOO'] = 'hello'

And it works perfectly.

So in which context do the JS interpreter trigger this error ?

The original code is written in Typescript, then transpiled with Babel. This is a runtime error. I assume this is not related to typescript since other people report a similar runtime error, ex. here and here

So in which context do the JS interpreter trigger this error ?

Strict mode .

 'use strict'; 'BAR'.FOO = 'test'; 

 'use strict'; var string = { name: 'bar' }; string.foo = 'hello'; console.log(string.foo + ' ' + string.name); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM