最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot create property 'FOO' on string 'BAR' - Stack Overflow

programmeradmin2浏览0评论

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

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

But Javascript pletely 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

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

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

But Javascript pletely 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

Share Improve this question edited Oct 6, 2017 at 12:07 user229044 239k41 gold badges344 silver badges346 bronze badges asked May 23, 2016 at 14:25 OffirmoOffirmo 19.9k13 gold badges81 silver badges99 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 13

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);

发布评论

评论列表(0)

  1. 暂无评论