M
M
Maxim Ivanov2016-06-09 17:37:03
JavaScript
Maxim Ivanov, 2016-06-09 17:37:03

How does undefined behavior in javascript stack up at the V8 level?

60b989b0efc64af8be490b92fe7054e1.jpg
Read about the oddities of JS. But how is this explained? Many of you at the university wrote in C ++. And we learned to define an overload of the operator of addition, subtraction, etc., however, we either handled undefined behaviors with exceptions, or somehow logically processed the input parameters of the operands (obj1 + obj2). But how is it processed?
After all, browser engines are written in C ++, which means that all this JS business is defined at the C ++ level, I wonder what internals determine this behavior. How can I understand such things?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2016-06-09
@splincodewd

however, we either handled undefined behaviors with exceptions, or somehow logically handled the input parameters of the operands (obj1 + obj2). But how is it processed?

Believe it or not, there are two types of programmers: those who think it's good to hush up mistakes, and those who ALREADY don't think so anymore. There are even articles on Habré that tell you which JS is good and "reliable". Considering the suppression of errors as reliable - IMHO this means not becoming (yet) a programmer. You can recover from mistakes, but you can't hide them.
As an example, look at the architectural approaches in Erlang. Just google for the words "erlang let it crash". This approach, supported by the Erlang process concept, says that code that can no longer run normally should "fall". And we will write another code that will monitor the work of the main one, and in case of abnormal situations, it will take the necessary actions (for example, restart the process).
Yes, although it’s far to go, the concept of exceptions in more popular languages ​​is also a way to learn to live with errors, not be afraid to generate them, and be able to handle them where possible and convenient . This is the first.
Second: JS is a loosely typed language. This is such a thing that allows you to write 1 + "1" and, without any overloads in the C ++ style, the compiler itself will cast some of the arguments to such a type so that the operation can be performed. You are now just very surprised that this is possible and that it could come to someone's mind (you came from the pluses, right?). By the way, PHP is almost the same. Only the transformation rules are slightly different. (By the way, read about the == and === operators in JS, you will learn a lot of amazing things if you haven’t come across it yet).
No internals define this behavior. All these cases are either explicitly described in the JS standard and simply implemented in accordance with the standard, or implemented in such a way that the implementations are as compatible as possible. Here you can read about typeof null returning "object" - in fact one big bug that is now supported so as not to break compatibility .
Because of these little things, JS has a minimum of consistency and consistency. This language is ruled by compatibility, not logic. Why is it the way it is, you have already been answered.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question