Javascript optional hint type

When a programming language is statically typed, the compiler can be more accurate in terms of memory allocation and, as a rule, more efficient (ceteris paribus).

I believe that ES4 introduced an optional hint type (from what I understand, Adobe had a huge role in contributing to its specification due to actionscript). Does javascript match the official hint type? Will ES6 support an optional hint of its own variables?

If Javascript supports type hinting, are there any tests that show how this pays off in terms of performance? I have not seen an open source project use this.

+7
source share
5 answers

My understanding, from listening to a lot of Javascript talk on different sites, is that hint-type won't do as much to help people think.

In short, most Javascript objects tend to have the same “shape” if you like. That is, they will have the same properties created in the same order. This "form" can be considered as the "type" of the object. Example:

function Point(x, y) { this.x = x; this.y = y; } 

All objects created from the Point will have the same “shape”, and the new internal Javascript mechanisms can make some fancy games to search faster.

In Chrome (possibly others), they use a high-bit flag to indicate whether the rest of the number is an integer or a pointer.

With all these bizarre things, what just prints for human coders happens. For example, I really do not need to worry about the type and not use this function.

However, you are half correct. The hinting type is part of ActionScript 3 , which is derived from ECMAScript , but the hint has never turned it into a standard. AFAIK, beyond wishful thinking, it was not discussed.

This video describes things in much more detail: http://www.youtube.com/watch?v=FrufJFBSoQY

+13
source

I'm late, but since no one answered your questions regarding standards, I will jump in.

Yes, the hinting type was discussed as part of ECMAScript 4, and it looks like it will be the future of JavaScript ... until ES4 releases the dust. ECMAScript 4 has been abandoned and has not been finalized. ECMAScript 5 (the current standard) did not contain many of the things that were planned for ECMAScript 4 (including the hint type), and was actually just a quickly updated version of the ECMAScript 3.1 project - to get some useful features from the door as a result of the untimely death of ES4.

As you already mentioned, they are now working on the release of ECMAScript 6 (which has some awesome features!), But don't expect to see hints like. The Adobe guys, to some extent, broke up with the ECMAScript committee, and the ES committee does not seem to be interested in his return (I think for a good reason).

If this is what you want, you can check TypeScript . This is a completely new Microsoft project, which is basically an attempt to be ES6 + types. This is a superset of JavaScript (almost identical except for the inclusion of types), and it compiles to run JavaScript.

+6
source

JavaScript JIT compilers have to do some fancy things to determine expression types and variables, since types are critical to many optimizations. But the authors of the JavaScript compiler have spent the last five years on all of this. Now compilers are really smart. Therefore, optional static types would not improve the speed of a typical program.

Surprisingly, type annotations in ActionScript sometimes make compiled code slower, requiring type checking (or implicit conversion) when a value is passed from untyped code to typed code.

There are other reasons why you might need static types in a programming language, but the ECMAScript standards committee is not interested in adding them to JS.

+4
source

ES7 (not soon) added new guard function. The syntax is now a bit like ES4 and TypeScript. Everyone uses : and add a type to the variable. But this does not confirm the syntax.

+4
source

Javascript is based on prototypes, so the "type" of an object is completely dynamic and can change over its lifetime.

Take a look at Ben Firschman's findings on Javascript performance regarding object types - http://jsconf.eu/2010/speaker/lessons_learnt_pushing_browser.html

0
source

All Articles