4 ms·
> Specifically, "accessing missing properties throws" will undermine how developers use the language. I couldn't agree more; there are many times I access a pr
by BinaryIdiot 12y ago
> Specifically, "accessing missing properties throws" will undermine how developers use the language.
I couldn't agree more; there are many times I access a property that may or may not exist and I plan on using the undefined that comes with it not existing. Requiring it to exist is trying to turn JavaScript into a more strict language without all of the language features to do so very easily.
- nawitus 12y agoI wonder if "SoundScript" will support optional properties. TypeScript also considers accessing missing properties a compile-time error, and I haven't noticed many problems with that as you can define some properties as optional.
- jahewson 12y agoThat would work at "compile" time, but how do we know whether or not a property is missing at runtime without accessing it? Or resorting to an expensive and minifier-hostile call such as hasOwnProperty? The TypeScript approach won't work here. Guaranteeing that optional properties have a default value would be one solution, another would be to use ES6 default parameters instead of options objects. But there has to be some sort of value to use at runtime if missing property access always throws.
- i_s 12y agoIt only throws if you access the property with the [] or . syntax, so I don't think this particular thing is a big problem.
- EdwardDiego 12y agoWhat other syntax is there to access a property?
- ianbicking 12y agoThey only barely mention it, but I think they expect you do to `foo = 'foo' in obj ? obj.foo : undefined` instead of `foo = obj.foo`
- jahewson 12y agoI really doubt it, as that's an unsound bypassing of the type system. Missing properties are slow in JavaScript, because the in-memory representation of the class has to be modified when new properties are added. Given that strong mode is about performance, it would make far more sense to just prohibit missing properties and encourage use of other ES6 language mechanisms such default parameters instead.
- deleted 12y ago[deleted]
- i_s 12y agoYou check if is there before you access it with . or [].
- EdwardDiego 12y agoOh right. I love those null/undefined checking chains, it really puts the Java in JavaScript.