How to use typed variables in javascript?

Is it possible to use typed variables in javascript? As an integer, string, float ...

+7
source share
10 answers

JavaScript variables are not changed.

JavaScript values, however. The same variable can be changed (a new value will be assigned), for example, from uninitialized to a number to a logical value in a string (not that you would like to do this!):

var x; // undefined x = 0; // number x = true; // boolean x = "hello"; // string 
+19
source

Javascript is dynamically typed , while other languages, such as C # and Java, are statically typed. This means that in Javascript variables, you can reassign values ​​of any type and, therefore, you do not need to explicitly specify the type of variables or the type of returned functions. If you saw such code in a statically typed language

 int x = 5; x = "hello"; 

you rightfully expect the compiler to start throwing a big nasty TypeError . Javascript, on the other hand, will successfully run this code, even if the type has changed.

 var x = 5; x = "hello"; 

Since variables can change types, the compiler cannot know so much information about them. You should expect Javascript tools to be worse than Java / C #, as much as subtleties such as code completion are useful. Fewer errors will be detected at compile time, and you will have to do more debugging at runtime than you probably used.

However, it also allows you to be more free with your variables, and you can change types as you wish, which is often convenient. You can write this code if you want:

 var x; //typeof x === "undefined" x = "Hello, world!"; //typeof x === "string" x = 42; //typeof x === "number" x = false; //typeof x === "boolean" x = {}; //typeof x === "object" 
+5
source

One of the main features of Javascript is that it is a weak typed language. Why do you need strong types?

+1
source

I recommend you read the following:

http://en.wikipedia.org/wiki/Strong_typing

javascript is a weak and dynamic type. This is dynamic because the type of the variable is determined at runtime and is inactive because you can perform this operation, for example,

 var letter = "2"; var number = 2; console.log(letter+number); 

this in java, C # or any other static and strict type, throws an error, but in javascript you get the result "22" (this is because javascript is weakly typed or weakly typed)

now..you have other languages ​​than continuing to use typed values, such as clojure or dart, where for performance reasons you can use functions or methods with typed arguments, javascript does not allow this and only accepts dynamic values ​​like ruby ​​...

I hope this help and you can understand my bad English: D

+1
source

Impossible in Javascript, but if you really need it, you should check TypeScript. This is a superset of Javascript that adds extra static typing. It also has class-based programming.

+1
source

Javascript is a freely typed language, so no, there are no types that are used in some other languages.

0
source

People writing β€œwhy not use it / you shouldn't use it” are wrong. The following Java Script 2.x specification has a plan for adding strong typed variables.

Meanwhile, you can use a very simple solution to emulate strong types:

 var = Object.create( String ); 

After that, autocompletion in many IDEs (including IntelliJ IDEA) will work fine, and you declared and initialized an object of the specified type.

Read more about my blog .

0
source

There is a simple hack for modeling typed variables in Javascript using typed arrays.

 var a = new Int8Array(1); a[0]=5; a[0]=~a[0]; // -6 var b = new Uint8Array(1); b[0]=5; b[0]=~b[0]; // 250 

The only useful reason that I know forever is when you need to use unsigned integer bitwise operations when translating code from a typed language that should do the same in Javascript. Javascript integers are signed by default, and bitwise operations can ruin a character.

Creating a typed array for just one variable is usually not suitable for performance.

Often...

 var b=5; b=(~b)&0xff; // 250 

will do the trick just fine, and only 32-bit unsigned integers should be introduced in Javascript to avoid bitwise operation problems.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays#Typed_array_views

0
source
 <html> <head> <meta charset="utf-8"> <title>JS_Byte</title> <script> class Byte { constructor(Value) { this.Number = new Uint8Array(1); this.Number[0] = Value; } get Get() { return this.Number[0]; } set Set(newValue) { this.Number[0] = newValue; } }; //On load function Load_Page() { let Byte_Num = new Byte(12); document.write(Byte_Num.Get.toString() + "<br>");// -> 12 Byte_Num.Set = 14; document.write(Byte_Num.Get.toString() + "<br>");// -> 14 Byte_Num.Set = 256; document.write(Byte_Num.Get.toString() + "<br>");// -> 0 } </script> </head> <body onload="Load_Page()"> </body> 

0
source

You can use TypeScript to define types for values

0
source

Source: https://habr.com/ru/post/910463/


All Articles