Home > Web Front-end > JS Tutorial > Typescript declares variables

Typescript declares variables

藏色散人
Release: 2019-04-15 14:23:21
Original
2521 people have browsed it

This article mainly introduces you to the relevant knowledge about typescript declaring variables. I hope it will be helpful to friends in need!

Variables:

A variable is a designated location in memory where some data/values ​​can be stored. According to the word variable, it can be said that the value of a variable can be changed.

When declaring variables, you must follow some rules:

Variable names can contain uppercase letters as well as lowercase letters and numeric letters. Variable names cannot start with a number. We can only use _ and $ special characters, other special characters besides these are not allowed.

Variable declaration: We can declare variables in many ways, as follows:

var Identifier:Data-type = value;

var Identifier: Data-type;

var Identifier = value;

var Identifier;

##var name:number = 10;The name here is a variable that can only store integer type data . var name:number;var name = 10;var name;Although the variable is declared here, we do not specify the data type or any value. The compiler then treats its data type as any data. By default, its value is set to undefined.
Variable declaration Description
The name here is a variable that can only store integer type data. But by default the value is set to undefined.

While declaring the variable here, we did not specify the data type. So the compiler determines its data type by looking at its value (i.e. number).

Variable scope in TypeScript:

This scope represents the visibility of the variable. Scopes define the variables we can access.

TypeScript variables can be in the following scopes:

Local scope: As a specified name, declared in a method, loop, etc. box. Local variables can only be accessed within the construct in which they are declared.

Global scope: If the variable is declared outside the constructor, then we can access the variable anywhere. This is called global scope.

Class Scope: If a variable is declared in a class, then we can only access the variable in the class.

Code example:

var global_var = 10         //全局变量  
class Geeks {  
   geeks_var = 11;             //类变量 
   assignNum():void {  
      var local_var = 12;    //局部变量  
   }  
}  
document.write("全局变量"+global_var)   
var obj = new Geeks();  
document.write("类变量: "+obj.geeks_var)
Copy after login

Output:


全局变量:10 
类变量:11
Copy after login

The above is the detailed content of Typescript declares variables. 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