Discuss some techniques in using JavaScript variable types
js supports the following types of variables: local variables, class variables, private variables, instance variables, static variables and global variables.
Local variables:
Local variables generally refer to variables that are valid within the scope of {}, that is, variables that are valid within the statement block, such as:
function foo(flag) { var sum = 0; if(flag == true) { var index; for(index=0;index<10;index++) { sum +=index; } } document.write("index is :"+index+"<br>"); return sum; } //document.write("sum is :" +sum+"<br>"); document.write("result is :"+foo(true)+"<br>");
This code The output results after execution are: "index is :undefined" and "result is :0". We can see that the value of the index variable we want to output is undefined, that is, undefined. Therefore, we can find that the index variable is destroyed after the if statement block ends. What about the "sum" variable? This variable is destroyed after the foo() function section is executed. If you remove the statement I commented and execute it again, you will find that the system will report an error. It is worth noting that if I change the foo() function above to the following:
function foo(flag) { var sum = 0; for(var index=0;index<10;index++) { sum +=index; } document.write("index is :"+index+"<br>"); return sum; }
You will be able to see that the index value ("index is :10") can be output. This is js and other Different places in the language, because index is defined outside the {} of the for loop, its scope is not destroyed until the foo() function is used.
Class variable:
Class variable is actually an attribute or field or a method of the class. This variable is automatically destroyed after an instance object of the class is destroyed, such as the Student we mentioned at the beginning kind. We won’t discuss this much, you can try it yourself.
Private variable:
Private variable is an attribute used internally by a class and cannot be called externally. Its definition is declared using var. Note that if you do not declare it with var, the variable will be a global variable (we will discuss it below), such as:
function Student(name,age,from) { this.name = FormatIt(name); this.age = age; this.from = from; var origName = name; var FormatIt = function(name) { return name.substr(0,5); } this.ToString = function() { return "my information is name: "+origName+",age : "+this.age+", from :" +this.from; } }
Here, we define two private variables, one origName and FormatIt() respectively ( According to the object-oriented interpretation, it should be called by the attributes of the class).
We also call the method in this case a variable, because the variable in this case is a function type variable, and function also belongs to the inheritance class of the Object class. In this case, if we define var zfp = new Student("3zfp",100,"ShenZhen"). But these two variables cannot be accessed through zfp.origName and zfp.FormatIt().
Note the following points:
1. Private variables cannot be indicated by this.
2. The call to a variable of private method type must be made after the method is declared. For example, we transform the Student class as follows:
function Student(name,age,from) { var origName = name; this.name = FormatName(name); this.age = age; this.from = from; var FormatName = function(name) { return name+".china"; } this.ToString = function() { return "my information is name: "+origName+",age : "+this.age+", from :" +this.from; } } var zfp = new Student("3zfp",100,"ShenZhen");
After the code is executed, an "object not found" error will be reported. This means that FormatName() is not defined.
3. Private methods cannot access the variable (public variable) indicated by this, as follows:
function Student(basicinfo) { this.basicInfo = basicinfo; var FormatInfo = function() { this.basicInfo.name = this.basicInfo.name+".china"; } FormatInfo(); } function BasicInfo(name,age,from) { this.name = name; this.age = age; this.from = from; } var zfp = new Student(new BasicInfo("3zfp",100,"ShenZhen"));
After executing the code, the system will prompt "this.basicInfo is empty or not an object "mistake.
The basic conclusion is that private methods can only access private properties. Private properties can be accessed anywhere in the class after being declared and assigned.
Static variables:
Static variables are owned by a class Owned attributes, access this attribute through class name + "." + static variable name. The following can be explained clearly:
function BasicInfo(name,age,from) { this.name = name; this.age = age; this.from = from; } BasicInfo.generalInfo = "is 3zfp owned object"; var basic = new BasicInfo("zfp",100,"ShenZhen"); document.write(basic.generalInfo+"<br>"); document.write(BasicInfo.generalInfo+"<br>"); BasicInfo.generalInfo = "info is changed"; document.write(BasicInfo.generalInfo+"<br>");
Execute the above code, you will get the following results:
undefined is 3zfp owned object info is changed
Note the following points:
1. Use the class name +"."+static variable name to declare a static variable
2. Static variables do not belong to the unique attributes of an instance object of the class, but are shared by the object.
3. Can be used as an instance Object name + "." + static variable name to access.
Global variables:
Global variables are variables with effective access control during the operation of the entire system. They are usually defined at the beginning of a js code, such as:
var copyright = "3zfp owned"; var foo =function() { window.alert(copyright); }
Note the following points:
1. If a variable is declared without var, it is considered a global variable. For example:
var copyright = "3zfp owned"; var foo =function(fooInfo) { _foo = fooInfo; document.write(copyright+"<br>"); } new foo("foo test"); document.write(_foo+"<br>"); 执行代码,将得到如下结果: 3zfp owned foo test 但是,这个又有一个注意的地方,function是编译期对象,也就是说_foo这个全局变量要在foo对象被实例化后才能被初始化,也就是说如果将 new foo(); document.write(_foo+"<br>"); 对调成 document.write(_foo+"<br>"); new foo(); 系统将提示 "_foo 未定义"。
2. If a local variable attribute with the same name as the global variable is defined, as follows:
var copyright = "3zfp owned"; var foo =function(fooInfo) { var copyright = fooInfo; //同名变量 this.showInfo = function() { document.write(copyright+"<br>"); } } new foo("foo test").showInfo(); document.write(copyright+"<br>");
When you execute the code, you will get the following results:
3zfp owned
foo test
The reason is that the function definition of variables is completed during compilation, that is, the definition of copyright inside foo is completed during compilation, and its scope is only valid within the foo object, and is not related to the external definition. The global variable copyright has nothing to do with it.
The above is the detailed content of Discuss some techniques in using JavaScript variable types. For more information, please follow other related articles on the PHP Chinese website!

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 mdf files and mds files With the continuous advancement of computer technology, we can store and share data in a variety of ways. In the field of digital media, we often encounter some special file formats. In this article, we will discuss a common file format - mdf and mds files, and introduce how to use them. First, we need to understand the meaning of mdf files and mds files. mdf is the extension of the CD/DVD image file, and the mds file is the metadata file of the mdf file.

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

After long pressing the play button of the speaker, connect to wifi in the software and you can use it. Tutorial Applicable Model: Xiaomi 12 System: EMUI11.0 Version: Xiaoai Classmate 2.4.21 Analysis 1 First find the play button of the speaker, and press and hold to enter the network distribution mode. 2 Log in to your Xiaomi account in the Xiaoai Speaker software on your phone and click to add a new Xiaoai Speaker. 3. After entering the name and password of the wifi, you can call Xiao Ai to use it. Supplement: What functions does Xiaoai Speaker have? 1 Xiaoai Speaker has system functions, social functions, entertainment functions, knowledge functions, life functions, smart home, and training plans. Summary/Notes: The Xiao Ai App must be installed on your mobile phone in advance for easy connection and use.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension
