Skip to content

Commit

Permalink
var-let-const
Browse files Browse the repository at this point in the history
  • Loading branch information
dptms committed Oct 22, 2017
0 parents commit 5f5f7a9
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions var-let-const.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# var
* `function scope` 函数作用域,如果在函数中声明的变量,外部是访问不到的。
* 声明的变量是可以重新覆盖定义。
```
var a = 4;
function test(){
var a = 3;
}
console.log(a); // 结果:4
```

# let
* `block scope` 块级作用域,在大括号中声明的变量,外部是访问不到的。 `const` 类似。

```
var price = 100;
var count = 10;
if(count > 5){
const discount = price * 0.6;
console.log(`The discount is ${discount}`); // 结果:The discount is 60
}
console.log(discount); // 结果:Uncaught ReferenceError: discount is not defined
```

* 在同一个作用域下,不能重复定义同一变量,`const` 类似。

# const
* `const``let` 不同的地方是,`let` 声明的变量是可以改变的,`const` 是不行的。

> 如果是引用类型的值
```
const person = {
name:'Dp',
age:30
}
person = {name:'tms'}; // 报错
person.age = 40; // ok
```
ps : 以上的例子一个是改变了对象,一个是改变了对象的属性,如果完全不想更改一个对象,可以使用`Object.freeze(obj)`

0 comments on commit 5f5f7a9

Please sign in to comment.