I think I like most of what this document proposes except for the following: In strong code, accessing objects (strong or not) throws on missing properties. New object properties have to be defined explicitly and cannot be removed from strong objects. To me, this seems to break a fundamental aspect of the language. I've found it very acceptable to be able to define an object literal property "on the fly." However, wi…
That's funny; I think that's the only part of what the document proposes I like. -- Well, okay, that's not really true; there are a lot of bits that just make sense, either because they hurt performance for little benefit (holes in arrays) or they're just dumb (arguments.caller). But I don't like gratuitously locking things down in a way that makes highly dynamically-typed code harder to write, where it doesn't seem…
A Strong Mode for JavaScript
21–30 of 45 posts
Re: A Strong Mode for JavaScript
#22Earlier quoted context omitted.
I think they're trying to push onto [Maps]. [Maps]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Yep, they're explicitly trying to do that. For what it's worth, you can still use "options hashes" in your argument APIs in strong mode using objects; you just write a library function something like: function options(options, defaults) { // the final args start off as a clone of the default args let args = Object.clone(defaults); // we then loop through the keys and copy in any overrides for(let key of Object.keys(a…
Map = new HashMap();
That declares a Map with a String key and Integer value. Is this what you're thinking of?Re: A Strong Mode for JavaScript
#23Earlier quoted context omitted.
Yep, they're explicitly trying to do that. For what it's worth, you can still use "options hashes" in your argument APIs in strong mode using objects; you just write a library function something like: function options(options, defaults) { // the final args start off as a clone of the default args let args = Object.clone(defaults); // we then loop through the keys and copy in any overrides for(let key of Object.keys(a…
The Java (5+) Map interface can be typed using generics (which -- if JS eventually introduces static typing -- I hope is implemented). Map = new HashMap (); That declares a Map with a String key and Integer value. Is this what you're thinking of?
interface BreadIngredientOptions {
flourType?: String; // this is the syntax for optional strings
sugarAmount?: String; // ditto: it's the ? that makes it optional
// ...
}
function bakeBread(ingredientOverrides: BreadIngredientOptions) {
// ...
}
// callers don't need to explicitly inherit or implement to be type checked
// however, since all properties are optional, this is less interesting
bakeBread({
flourType: 'white'
});
But you can do even better than that example shows. One common problem with maps-as-named-arguments is that you can't easily determine which arguments are required and which are optional. With typed optional properties and structural subtyping you can enforce that at compile time, as follows: interface MyArgumentInterface {
requiredArg: number;
optionalArg?: number;
}
function f(args: MyArgumentInterface) {
// ...
}
// This works:
f({
requiredArg: 10,
optionalArg: 5
});
// This also works:
f({ requiredArg: 50 });
// This statically throws at compile time:
f({ optionalArg: 10 });
It's a combination of the simple object literal syntax from raw JS that makes it easy to create objects of arbitrary types, with structural subtyping. I'm not aware of any language with the same features (but would love to be corrected!).Re: A Strong Mode for JavaScript
#24> It is a SyntaxError to use the keyword 'var' What if I want a variable?!
use `let ` or `const `. there's no need to use `var` in ES6+, ever.
Re: A Strong Mode for JavaScript
#25Earlier quoted context omitted.
use `let ` or `const `. there's no need to use `var` in ES6+, ever.
So, if I want get the sum of an array of numbers, the only option is to use recursion or a reduce function? Is there a way to accomplish this using a for loop while sticking to let keywords?
let result = 0;
for (let i = 0; i Re: A Strong Mode for JavaScript
#26I think I like most of what this document proposes except for the following: In strong code, accessing objects (strong or not) throws on missing properties. New object properties have to be defined explicitly and cannot be removed from strong objects. To me, this seems to break a fundamental aspect of the language. I've found it very acceptable to be able to define an object literal property "on the fly." However, wi…
Being able to do foo = bar.x || 3; rather than foo = bar.hasOwnProperty('x') ? bar.x : 3; is nice.
foo = ('x' in bar) ? bar.x : 3;
instead. The problem with your code is that if the property bar.x exists, but is one of any number of values, like 0 or false, your code will still set foo to 3. Requiring properties to be explicitly created means that you're separating existence from value, which are two very different things in my book.Re: A Strong Mode for JavaScript
#27Earlier quoted context omitted.
Being able to do foo = bar.x || 3; rather than foo = bar.hasOwnProperty('x') ? bar.x : 3; is nice.
Personally, I would say foo = ('x' in bar) ? bar.x : 3; instead. The problem with your code is that if the property bar.x exists, but is one of any number of values, like 0 or false, your code will still set foo to 3. Requiring properties to be explicitly created means that you're separating existence from value, which are two very different things in my book.
Re: A Strong Mode for JavaScript
#28I think I like most of what this document proposes except for the following: In strong code, accessing objects (strong or not) throws on missing properties. New object properties have to be defined explicitly and cannot be removed from strong objects. To me, this seems to break a fundamental aspect of the language. I've found it very acceptable to be able to define an object literal property "on the fly." However, wi…
It does break fundamental uses of the language. Throwing on missing properties is a near-hostile change for most current JS developers. The given justifications for doing this, though, seem to be all about performance rather than an attempt to evolve the language. If that's true, I think it's possible the proposal has a naming problem. Calling it "use strong" implies that this is about evolving the JS language so tha…
Re: A Strong Mode for JavaScript
#29My biggest problem with this proposal is that it will force a full-blown code-style on you. Many, perhaps most, of the features will be part of the common consent how to write good apps but I am sure lots of people have a problem with feature x or y. I wonder if a "selecting a subset" would be practicable.
Re: A Strong Mode for JavaScript
#30Earlier quoted context omitted.
It does break fundamental uses of the language. Throwing on missing properties is a near-hostile change for most current JS developers. The given justifications for doing this, though, seem to be all about performance rather than an attempt to evolve the language. If that's true, I think it's possible the proposal has a naming problem. Calling it "use strong" implies that this is about evolving the JS language so tha…
I fail to see how it breaks anything? It is opt-in. Aside from that "use optimize" is indeed a better naming.
"use strong"
try {
foo = bar[maybeMissing]
} catch {
// only runs in strong mode
}
The above will take different code paths depending on whether your JS engine supports "strong" mode.