The previous article talked about some concepts (lexical structure) and data types (part) in js. Let’s continue in this chapter and then learn about the scope of operating data and functions in js.
1. Conversion between objects and basic types:
Whenever, as long as the object is not empty, it is true in a Boolean environment.
For example;
new Boolean(false);
new Number(0);
new String("");
new Array();
Although the internal value above is false, the value of the object is true;
Object ? valueOf() ? toString()
Among them, the Date class performs toString() conversion first.
2. Manipulating a data value in js:
Any language has its own method of manipulating data;
Js is no exception. There are 3 important ways to manipulate a data value in js.
1) Copy it. For example, assign it to a new variable.
2) Pass it as a parameter to a function or method.
3) Can be compared with other values.
Js operates the values of these data through two methods: passing by value and passing by address.
As you can see from the name, passing by value is to manipulate data by passing values. During the assignment process, the actual value is copied and stored in a new variable. The copied value and the original value are two completely independent values. So if you change the copied value, it won't affect the original value. When comparing sizes, a byte-by-byte comparison is usually performed.
Passing address As the name suggests, it is to manipulate data by passing the address. During the assignment process, the address of the actual value (it can be said to be a reference) is copied. They are not completely independent, so if you change the value through the reference, the original value will also change. When comparing sizes, they are usually compared to see if they reference the same address.
Simple address passing example:
var a = new Date();
alert(a.getDate());
var b = a;
b.setDate(21);
alert(a.getDate()) // Output 21
3, generally speaking:
Basic data types are operated by passing values. (If you forget which basic data types are, you can look back.)
Object data types are operated by passing addresses. (Such as arrays and functions)
Example:
<script> <br/>//Pass value<br/>a=1; <br/>b=a; <br/>b=2; <br/>alert (a); //Output 1 <br/><br/>//Address <br/>x=[1,2]; <br/>y=x; // What is assigned to y is only a reference to x, not x itself. The array has been assigned in the statement. After executing this code, there is still only one array object, but we have two references to it. <br/>y[0]=2; <br/>alert(x[0] " | " x[1]); //Output 2 | 2 <br/></script>
We must pay attention to the characters String:
Strings in js are copied and passed by address, and they are compared by value.
Objects and arrays are passed by value, but the value passed is actually a reference, not the object itself.
Summary: Type Copy and Communication Comparison
Digital Digital Value Value Value
Boolean Dive Value Value Value
Strough Strings Invasion and Uncosty Value The address passed
is immutable: In JS, there is no way to change the content of the string value.
For strings, it doesn’t make much sense whether to pass by value or by address.
4. Garbage collection mechanism:
Automatically release memory in Js.
For example:
var s = “heelo”;
var b = s.toUpperCase();
s=b; //After running here, js will automatically detect that a certain Object, because s=b, js will automatically release the storage space occupied by the string "heelo". That is, we can no longer get the original "heelo" value;.
5, javascript variable:
Js is untyped. Its variables can hold any type of value.
Variable declaration:
var a ;
var b ;
or
var a, b ;
or
var a=0, b=1;
Duplicate declarations are legal.
If a declaration is omitted, js will declare the variable implicitly. Of course implicitly declared variables are always global variables.
6, variable scope:
Js has two types: global and local.
From the definition of the name, we can know that the scope of global variables is global.
In js code, there are definitions everywhere.
The scope of local variables is local.
Defined in the function body.
Local variables with the same name have a higher priority than global variables with the same name. The following example illustrates this:
var a ="abc"; //Global variables
function check(){
var a = "efg"; //Local variable with the same name
document.write(a);
}
check(); // Output efg
Look at a more classic example:
var scope = "global";
function f(){
alert(scope) ; //Output undefined
var scope = "local";
alert(scope); //Output local
}
f();
Why is the first one output? What about undefined?
Because js stipulates that when the names of local variables and global variables are the same, the global variables with the same name in the function body will be hidden.
Then the example just now is actually equivalent to:
function f(){
var scope;
alert(scope);
scope = "local";
alert(scope);
}
f();
OK, if you understand this example, it means that you have a little understanding of some differences between local and global.
7, variable scope:
From inside to outside:
Lexical scope Scope chain Variable lookup
var x = 1;
function f(){
var y =2;
function g(){
var z =3;
}
} Call g() object; z =3;
Call f() object; y =2;
Is the global variable x = 1 defined here?
Yes
No
Get value
Is it defined here?
Yes
No
Get value
Is it defined here?
Yes
No
Get value
Undefined
8, Client global variables:
In client js, the Window object represents the browser window, which is a global object. ,
For example; the commonly used parseInt() and Math() are properties defined by the Window object.
Js allows multiple execution environments of global variables, and each environment has different global objects.
For example: each independent browser window of client js, or different frames of the same window.
The code in it all runs in its own execution environment and has its own global object.
Of course, you can use the expression parent.frames[0].x ; to refer to the global variable x in the first frame; this connects the code in different frames.
But there is a security issue here.
Summary;
Mainly talks about "passing by value and passing by address" and the scope of functions.
Slightly difficult to understand for novices. If you still don’t understand, you can search for information on google

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor