JavaScript pre-parsing and related skills analysis_javascript skills
This article describes JavaScript pre-parsing and related techniques with examples. Share it with everyone for your reference, the details are as follows:
Variable
Similarly, start with the error comparison tips of these two small examples.
1 2 3 4 |
|
Think about it first, why one prompts undefined and the other throws an error of undefined variable. . Let’s first look at the JavaScript parsing process.
Javascript will do an event "pre-parsing" before executing the process. The parsing engine will perform the creation of all var variables at the block level and give them an initial value: undefined. In this way, it is obvious why undefined pops up in the first example.
So the first piece of code is actually equivalent to
1 2 3 |
|
Then why does the second piece of code throw an error again? It is no longer in the "pre-parsing" stage. (Here I assume that the browser only does two things when it encounters a script tag: pre-parsing and execution. , in fact, it is not only these two things), but in the execution stage, the reason why the error is thrown is that js does not know the status of y2 in the execution section state (the pre-parsing stage does not capture any information about y2), of course, an undefined error is thrown information. This involves another problem: js is a weakly typed language, and variables can be used without being defined, so why is a definition error thrown here?
Everything happens for a reason. JavaScript always has many strange characteristics of its own, such as the uneven reading and writing of variables. Undefined variables are only writable, not readable. What is writable? Everyone is familiar with this way of writing:
1 2 |
|
But when reading it, the js engine cannot find any relevant information about it, so it acts according to its own temper and throws an undefined error without mercy. This is the game rule of js. And but, why can we get its type? Remember how js operates on objects. If you access a non-existent property and type of the object, you will be prompted with undefined (because it is currently a property of the window object).
Another note: A distinction needs to be made here. The reading and writing of variables are not equal. It is only used for variables. It reads the properties of all objects. This feature does not exist. If it does not exist, it will prompt undefined.
Conclusion
Here, the result of my thinking: There are certain similarities between the writing operations of variables and objects. However, each has its own set of rules for reading operations. Because of this, the above problem arises.
In this way, it should be easy to get the answer to the following question.
1 2 3 4 |
|
Function
By extension, function. Remember the pre-parsing mentioned above. In the pre-parsing of javascript, in addition to the pre-definition of the var variable, it also includes the extraction of the definition of the function, so the function can be defined anywhere in the script. , called anywhere. Not limited to what it was before.
But the way to define functions includes a method called literal definition, which uses the var method to declare functions. See below
1 2 |
|
Remember this convention: the call must appear after the statement. Why? If you understand the above, the answer here is already clear. The javascript engine will give them an initial value of undefined when pre-parsing var. In this way, if we call it before its declaration, the javascript engine has not yet gotten its true value, and will naturally report "xxx is not a function" Wrong. This also clarifies why they are both function declarations, but one is related to the order of declaration and calling, while the other has no such constraints.
Conclusion
It is a function, the result of js execution and dynamic modification, and still follows the pre-parsing rules of variables (when alerting above, it did not get the information of the literal function).
What if the two are mixed. See below, there are both variables and functions for y4.
1 2 3 4 5 |
|
Because the declaration priority of function in JavaScript pre-parsing is high, y4 is naturally of function type, but after y4 is assigned a value (the js engine is in the execution process at this time), its assignment operation to js will be Override the function's declaration. So:
1 2 3 4 5 6 |
|
The first alert result is function because it is at the top of the js execution process. The second time it alerts, its value has been rewritten to 5 (don’t be fooled by the function’s definition position below.)
Thinking about the parsing and execution of js separately, I found that my eyes suddenly became enlightened, and the answers to many questions naturally emerged. As the author of that article said, "Once you understand the execution environment, calling object, closure Concepts such as packages, lexical scope, and scope chains can easily solve many phenomena in the JS language. "
Looking now, even in this language where there are many incredible things, there are many reasons that can be traced back to it.
How to make better parameter judgment
Having discussed so much above, how can we make it closer to actual development? Since JavaScript has uneven reading and writing, how can we avoid making parameter judgments without reporting errors?
eg:
1 |
|
如何严谨一些呢。
1 2 3 4 |
|
最后补充又一个小quiz, (理解 预解析与执行的分离)
1 2 3 4 5 6 |
|
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
