Is JavaScript a type-safe language? -


i have read javascript not type-safe language, not sure how true that.

say have following code:

<script>     var = 123;  // int     i();  // treat function (this produce error) </script> 

when run code following error:

enter image description here

so not allowed treat int variable function, doesn't means javascript type-safe language?

type safety complex topic , there's no 1 agreed definition of "type-safe" language is. definition of it, no, javascript not type-safe. :-) in particular example, though, javascript did provide runtime type safety: didn't try call i , cause kind of memory access exception or similar; instead, when code tried call it, first thing javascript engine did check see if callable and, since isn't, raised protective error.

but type-safe language tries discourage or prevent errors or undesireable behavior due using incorrect type, through type enforcement (both @ compilation/parsing stage , when code runs). javascript doesn't (the above notwithstanding); in general, javascript tends coerce instead.

for instance, in type-safe language, fail:

console.log("hi there" * 4);

...assuming * isn't defined operator strings. (i believe there's @ least 1 language , result in "hi therehi therehi therehi there").

but in javascript, * doesn't have defined meaning strings. rather causing error (at compilation/parsing stage or when run), string implicitly converted number n, , used in expression n * 4. in case of string "hi there", coercion results in value nan ("not number") rather causing error (and nan * 4 results in nan).

type-safe languages typically (though don't think always) have typed variables/parameters/properties , similar , @ least type checking @ compilation/parsing stage rather when relevant code runs. in languages, i have had type associated (e.g., int i rather var i), , code trying call function have failed @ compilation/parsing stage, rather later when run in javascript. javascript, on other hand, doesn't have typed variables/parameters/properties @ all. variable can hold object 1 moment , primitive number next.

one of benefits of javascript friendly duck-typing (if looks duck , quacks duck, it's duck). instance, suppose have function that, notionally, requires string:

function capitalize(str) {     return str.charat(0).touppercase() + str.substring(1); } 

in javascript, following code calling it:

capitalize(42); 

is correct , not raise error when code containing call compiled/parsed. will raise error when code called — not because 42 isn't string (it isn't, that's not point), because 42 doesn't have charat method.

in language static type safety (e.g., compilation/parsing stage type safety), there'd type information associated str argument , error when code compiled/parsed.

but in javascript, not happy compile/parse code, it's happy run on non-string provided whatever give meets these criteria:

  1. it has charat method returns touppercase method, and

  2. it has substring method.

as long give meeting criteria, whether thing string or not, it'll work.

function capitalize(str) {      return str.charat(0).touppercase() + str.substring(1);  }    var thingy = {      charat: function() {          return {            touppercase: function() {              return 40;            }          }      },      substring: function() {          return 2;      }  };    console.log(capitalize(thingy)); // 42

;-)


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -