Home >Web Front-end >Vue.js >How to declare variables in vue.js

How to declare variables in vue.js

coldplay.xixi
coldplay.xixiOriginal
2020-12-07 09:39:375954browse

Vue.js method of declaring variables: 1. Use let to define, let is a block-level scope. After using let to define the function internally, it has no impact on the outside of the function; 2. Use var to define, and var-defined variables Can be modified; 3. Use const definition, variables defined by const cannot be modified.

How to declare variables in vue.js

The operating environment of this tutorial: Windows 7 system, Vue version 2.9.6, Dell G3 computer.

【Recommended related articles: vue.js

vue.js method of declaring variables:

1. Use let to define

let is a block-level scope. After using let to define the function inside, it will have no impact on the outside of the function.

let c = 3;
console.log('函数外let定义c:' + c);//输出c=3
funcion change(){   
    let c = 6;
    console.log('函数内let定义c:' + c);//输出c=6
}
change();
console.log('函数调用后let定义c不受函数内部定义影响:' + c);//输出c=3

2. Use var Definition

Variables defined by var can be modified. If not initialized, undefined will be output and no error will be reported

var a = 1;
// var a;//不会报错
console.log('函数外var定义a:' + a);//可以输出a=1
function change(){  
    a = 4;
    console.log('函数内var定义a:' + a);//可以输出a=4
}
change();
console.log('函数调用后var定义a为函数内部修改值:' + a);//可以输出a=4

3. Use const definition

Variables defined by const cannot be modified and must be initialized

const b = 1;    //正确
const b;    //错误,必须初始化
console.log('函数外const定义b:' + b);/有输出值
//b = 5;
//console.log('函数外修改const定义b:' + b);//无法输

Related learning recommendations: javascript video tutorial

The above is the detailed content of How to declare variables in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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