JavaScript Advanced Programming: Variables and Scope

JavaScript Advanced Programming: Variables and Scope

1. Original value and reference value

The values ​​of the six simple data types are all primitive values. When a primitive value is assigned to another variable through a variable, a new value will be copied, and the two are independent of each other.

let num1 = 5
let num2 = num1


When a reference value is assigned to another variable through a variable, a value is also copied. This value is actually a pointer (reference), and the pointer still points to the same object.

let obj1 = new Object()

let obj2 = obj1

Since they point to the same reference object, adding properties to obj1 will also affect obj2 .

obj1.name = "zhangsan"

console.log(obj2.name) // zhangsan


When passing parameters to a function, there is only one situation where the parameters are passed by value, which is the same as assigning a variable. However, with reference values, the value passed is a pointer, but the pointer still points to the same object.

2. instanceof

typeof is generally suitable for judging primitive values, but not for reference values, because null and other objects return objects, and the specific type of the object is unknown. The best way to determine what type of object the reference value is is to use instanceof expression.

console.log(1 instanceof Object) //false
let obj = new Object();
console.log(obj instanceof Object) //true
console.log(null instanceof Object) //false



function fun(){
    //
}
console.log(fun instanceof Object) //true
console.log(fun instanceof Function) //true

console.log([] instanceof Object) //true
console.log([] instanceof Array) //true

3. Scope

The scope of a variable is called a scope or execution context. A variable is no longer visible outside of the scope. All systems have a scope chain when searching for a variable. First, look for the variable in the closest block scope that references it. If it is not found, continue looking in the outer local scope. If it is not found again, look in the global scope. If it is not found, an error ReferenceError: xxx is not defined will be reported.

This is the end of this article about variables and scope in advanced JavaScript programming. For more information about JavaScript variables and scope, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Let’s learn about JavaScript variable scope
  • Detailed explanation of variable scope in JavaScript
  • Do you know variable declaration in JavaScript?
  • Detailed examples of variable and function promotion in JavaScript
  • How to turn local variables into global variables in JavaScript
  • JavaScript variables and transformation details

<<:  TABLE tags (TAGS) detailed introduction

>>:  Designing the experience: What’s on the button

Recommend

Example of nginx ip blacklist dynamic ban

When a website is maliciously requested, blacklis...

MySQL query syntax summary

Preface: This article mainly introduces the query...

The unreasonable MaxIdleConns of MySQL will cause short connections

1 Background Recently, some performance issues ha...

Understand CSS3 FlexBox elastic layout in 10 minutes

Basic Introduction Features Flexbox is a CSS disp...

Linux installation apache server configuration process

Prepare the bags Install Check if Apache is alrea...

Advertising skills in the Baidu Union environment (graphic tutorial)

Recently, students from the User Experience Team o...

Detailed explanation of Vue routing router

Table of contents Using routing plugins in a modu...

Mini Program to Implement Sieve Lottery

This article example shares the specific code of ...

Summary of common Mysql DDL operations

Library Management Create a library create databa...

Installation and use of Ubuntu 18.04 Server version (picture and text)

1 System Installation Steps OS Version:1804 Image...

MySQL select, insert, update batch operation statement code examples

In projects, batch operation statements are often...

Installation method of mysql-8.0.17-winx64 under windows 10

1. Download from the official website and unzip h...