Home > Web Front-end > JS Tutorial > body text

In-depth understanding of left query and right query in javascript

零到壹度
Release: 2018-04-04 10:41:49
Original
3989 people have browsed it

This article mainly introduces the in-depth understanding of left query and right query in JavaScript. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor to take a look

Value and reference

Scope

  • In a narrow sense, a scope is an object (more specifically, it should be a collection);

  • In a broad sense, a scope is It is a set of rules for storing variables and making it easy to find these variables later;

    • The scope is responsible for collecting and maintaining the scope consisting of all declared identifiers A series of queries and enforces a very strict set of rules that determines the access rights of the currently executing code to these identifiers.

Context

  • Preparation before execution of code (global code, function body, eval code):

    • 1. Promotion (variable function function expression);

    • 2. Determine the point of this;

    • 3. Associated with the corresponding scope;

    • If the code segment is a function body, then on this basis, additional: parameter assignment, arguments assignment;

  • The relationship between scope and execution context:

    • A scope may contain several contexts. It is possible that there has never been a context (the function has never been called); it is possible that there has been, and now after the function is called, the context is destroyed;

Left/right query

  • Left query:

    • On the left side of the assignment symbol, who is the target of the assignment operation;

    • The relationship between the actual and formal parameters when the function is called is a left query;

    • The query of the variable on the left side of the equal sign. In the entire scope chain, if no declaration of a variable is found, the js engine will automatically declare a variable with the same name globally; however, the declaration of this variable will not be promoted.

      (function(){
          function test(a){
              b=a;
              console.log(b);//2
          }
          test(2);
      })();
      console.log(b);//2
      Copy after login
  • Right query:

    • The non-left side of the assignment symbol, who is the source of the assignment operation ;

    • Query for variables other than the left side of the equal sign. In the entire scope chain, if no declaration of a variable is found, a ReferenceError error will be thrown directly;

      console.log(a);//ReferenceError: a is not defined
      Copy after login
    • Special right query

      //a并未定义赋值
      console.log(typeof a);//undefined
      Copy after login
  • Strict mode & non-strict mode

    • ##RHS and LHS will throw ReferenceError exception

    • ReferenceError exception is the same as scope determination failure Related, TypeError means that the scope judgment is successful, but the operation on the result is illegal or unreasonable;

    • The right query cannot be found in all nested scopes of the query If the required variables are required, the engine will throw a ReferenceError exception, but it should be noted that no exception will be reported when typeof is used;

    • The left query cannot be found in all nested scopes of the query For the required variable, a variable with that name will be created in the global scope;

    • If the variable has not been declared (the variable cannot be found in any scope) Below, the behavior of these two queries is different

    • Non-strict mode:


    • Strict mode:


  • typeof's security prevention mechanism

    • 1. Use the global variable DEBUG in the program as the "debug mode" switch, Our declaration in the DEBUG.js file var DEBUG =true; this file is only loaded into the browser during development and testing, and will not be loaded in the production environment;

      if(DEBUG){ //在生产环境中会报错
               console.log("开始调试");
        }  if(typeof DEBUG !== "undefined"){
                console.log("开始调试");    
        }
      Copy after login

    • 2 .Write a polyfill (padding code to supplement the missing functions in the current running environment) for a missing function

      if(typeof polyfill_a === "undefined"){        //注意这一块不需要var,跟变量的提升有关
                 //这一块需要使用函数表达式而不是函数声明
                 polyfill_a = function(){
                    //功能代码
                 }
         }
      Copy after login

Related recommendations:

Left query and inner query

The difference between left query and right query

The above is the detailed content of In-depth understanding of left query and right query in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!