Forum Bugs

Javascript string character access returns 'undefined'

ehzhang
Hello!

I'm currently working on a project involving adding a React component to a page, and I've been tracking down an error that returns `TypeError: undefined value is not an object`.

In the bundled javascript, prince shows the error to occur here:
https://github.com/facebook/react/blob/b1b4a2fb252f26fe10d29ba60d85ff89a85ff3ec/src/renderers/dom/shared/eventPlugins/SimpleEventPlugin.js#L126

Upon inspecting the values, I found that the string `event` has `typeof` `"string"`, but `event[0]` is `undefined`, which causes an error when trying to call `toUpperCase()`.

I'm uncertain whether to classify this as a bug or a feature request, as character access is supposed to be included in ES5 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Description)
ehzhang
For anyone else looking to use React, I've elected to render all of my React components server-side, rather than chase down the necessary polyfills, which is now rendering as expected :)

mikeday
Prince doesn't support the A => B function syntax yet, so I don't think this script will run.
ehzhang
Yes, that is true - though the code we were supplying to prince was a transpiled bundle (using babel) that doesn't transform that string indexing. When I changed that one line to use charAt instead, it rendered fine (with the use of some polyfills as mentioned in another thread).

Thanks for responding! It seems like this might be more of a really-low-priority feature request, as it seems like string indexing is a super nice-to-have when charAt is available.
hallvord
Hi ehzhang, thanks for doing the analysis - I've added a small test case to the "miscellaneous testcases" repo as a reminder :)
https://github.com/yeslogic/miscellaneous-testcases/blob/master/scripting/string-array-style-indexing-001.htm
(The test covers two "array-like" ways strings can be used in modern-ish JS engines. @mikeday and co might consider those different features, if they do I should split the test into two.. Also, in disclaimerish mode: I can't predict when devs will get around to implementing something here nor how long it might take.)

Announcement: repos for tests/utils

mikeday
I've fixed this now and it will be available in the next build.
ehzhang
Super! Thank you :)
mikeday
The latest build now supports array-like string access. :)