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;
Variable declaration | Description |
The name here is a variable that can only store integer type data . | |
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). | |
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 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)
全局变量:10 类变量:11
The above is the detailed content of Typescript declares variables. For more information, please follow other related articles on the PHP Chinese website!