


A brief introduction and examples of variable scope, value transfer, and address transfer in JavaScript Basics_javascript skills
javascript:变量的声明
以下是几种声明变量的方式
var value;
var value,value1,value2;//同时声明多个变量,但是这些变量的值都是undefined
var i = 0,j = 0,k=100;//变量声明,初始化一体。
//如果大家尝试读一个不存在的变量(值)会报错!但是尝试给一个未使用Var声明的变量赋值,javascript
//会隐式的声明改变量,而且声明了的变量还是全局的。细节:所以大家创建变量都尽量使用Var
//变量的作用域(这个问题也容易出,大家要搞明白)
javascript:变量的作用域
这些都是细节,和我一样初学的一定要注意避免!
var golbal = "golbal"; //全局变量
var local ="local";
function area()
{
//局部变量的优先级比全局变量的高
var local = "arealocal"
//当函数体内声明的变量名和全局变量名相同时,javascript 会隐藏全局变量
var golbal ="areagolbal";
document.write("local is :"+local + "and golbal is :" + golbal +"
");
}
area();
//输出:local is :arealocaland golbal is :areagolbal
在嵌套的函数里面定义局部变量,效果会怎么样呢?看下面:
var hope = "moremoney";
function createmore()
{
var hope = "have more money";//局部
function createmoreto()//嵌套函数
{
var hope = "have more money to much";//局部
document.write("Createmoreto hope is :"+hope +"
");
//输出:Createmoreto hope is :have more money to much
}
createmoreto();//调用
document.write("Createmore hope is :" +hope +"
");
//输出:Createmore hope is :have more money
}
createmore(); //调用
javascript:传值和传址
这里也是比较重要的概念!不要漏了。
传值 | 传址 | |
复制 | 实际复制的值,存在不同的、独立的拷贝。 | 复制的只是对数字的引用。如果通过这个新的引用修改了数值,这个改变对最初的引用来说也是可见的。 |
传递 | 传递给函数的是值的独立拷贝对它的改变在函数外没有影响 | 传递给函数的是对数值的引用,如果函数通过传递给它的引用修改了数值,这个改变也是可见的。 |
比较 | 比较这两个对立的值,通常逐字节的比较,以判断是否相等 | 比较的是两个引用,以判断它们引用的是否是同一个数值。 |
javascript: basic types and reference types
The basic rules of JavaScript are: basic types are operated by passing by value, and reference types are operated by passing by address. (For what is a value type, or what is a reference, please see my previous article)
Pass by value
var value = 1;
var copyvalue = value; //Assign value to another variable
function addTotal(total,arg)
{
total = arg; //total = total arg has the same effect as
}
//Call the function and pass two parameters (you may think that this function changes the value of the global variable, but it does not. The function also uses opposite copy)
addTotal(value,copyvalue);
if(value == 1) copyvalue = 2;
document.write("total t" value "and copyvalue tt" copyvalue "
");
//Final output: total 1and copyvalue 2
Pass by address
var array = new Array("Javascccp");
var objarray = array;
function modifyArray(arr)
{
arr[0] = "JAVASCRIPT";
}
//Before calling the function
document.write(array[0] "
//Output Javascccp;
//After calling the function
modifyArray(array);
document.write(array[0] "
");
//Output uppercase JAVASCRIPT
//The same effect will be achieved by modifying objarray
objarray[0] = "Frank";
document.write(array[0] "
");
//Output Frank;
Summary: I hope everyone will not miss the above content, it is still very helpful for learning the following knowledge!

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



typedef struct is used in C language to create structure type aliases to simplify the use of structures. It aliases a new data type to an existing structure by specifying the structure alias. Benefits include enhanced readability, code reuse, and type checking. Note: The structure must be defined before using an alias. The alias must be unique in the program and only valid within the scope in which it is declared.

Variable expected value exceptions in Java can be solved by: initializing variables; using default values; using null values; using checks and assignments; and knowing the scope of local variables.

Advantages of JavaScript closures include maintaining variable scope, enabling modular code, deferred execution, and event handling; disadvantages include memory leaks, increased complexity, performance overhead, and scope chain effects.

The #include preprocessor directive in C++ inserts the contents of an external source file into the current source file, copying its contents to the corresponding location in the current source file. Mainly used to include header files that contain declarations needed in the code, such as #include <iostream> to include standard input/output functions.

Life cycle of C++ smart pointers: Creation: Smart pointers are created when memory is allocated. Ownership transfer: Transfer ownership through a move operation. Release: Memory is released when a smart pointer goes out of scope or is explicitly released. Object destruction: When the pointed object is destroyed, the smart pointer becomes an invalid pointer.

Can. C++ allows nested function definitions and calls. External functions can define built-in functions, and internal functions can be called directly within the scope. Nested functions enhance encapsulation, reusability, and scope control. However, internal functions cannot directly access local variables of external functions, and the return value type must be consistent with the external function declaration. Internal functions cannot be self-recursive.

In Vue, there is a difference in scope when declaring variables between let and var: Scope: var has global scope and let has block-level scope. Block-level scope: var does not create a block-level scope, let creates a block-level scope. Redeclaration: var allows redeclaration of variables in the same scope, let does not.

In JavaScript, the pointing types of this include: 1. Global object; 2. Function call; 3. Constructor call; 4. Event handler; 5. Arrow function (inheriting outer this). Additionally, you can explicitly set what this points to using the bind(), call(), and apply() methods.
