Open greglittlefield-wf opened 3 years ago
I did a little more research, and apparently in JS, when you're using null
as a key, it actually gets coerced to a string first.
So, my above statement of "null is a valid property name in JavaScript" is incorrect, since property names must be strings; sorry about that!
Source: the ES6 spec:
Example illustrating this behavior in JS:
let object = {};
object[null] = 'test';
Object.keys(object); // ['null']
object[null]; // 'test'
object['null']; // 'test'
So one workaround to this issue is to use the string 'null'
instead of null
.
For instance, in a function that takes in a potentially nullable name:
void addProperty(Object object, Object? name) {
setProperty(object, name ?? 'null', 'foo');
}
That gets roughly gets compiled to:
var t1 = name == null ? "null" : name;
object[t1] = 'test value';
which I wouldn't expect to add significant overhead.
Given that, perhaps this issue could be closed?
Dart SDK version: 2.12.0
For reference, the JS utilities from dart:js_util in question:
The
name
argument inhasProperty
/getProperty
/setProperty
and themethod
argument incallMethod
should beObject?
instead ofObject
, sincenull
is a valid property name in JavaScript.The following code currently compiles and runs correctly in dart2js, even though there are analysis errors:
...and has the output I'd expect: