This article is a reprinted article
Original link: https://blog.csdn.net/unionz/article/details/80032048
First, a common question is, what is the relationship between ECMAScript and JavaScript?
ECMAScript is an internationally adopted standardized scripting language. JavaScript consists of ECMAScript, DOM and BOM. It can be simply understood as: ECMAScript is the language specification of JavaScript, and JavaScript is the implementation and extension of ECMAScript.
In 2011, ECMAScript version 5.1 was released. Most of us used ES5 before
ECMAScript 6 was officially adopted as an international standard in June 2015.
1. Block level scope {}
Scope in ES5 includes global scope and function scope. There is no concept of block scope.
Block level scope is added in ES6. The block scope consists of {}, and {} in the if statement and for statement also belongs to the block scope.
- <script type= "text/javascript">
- {
- var a = 1;
- console.log(a); // 1
- }
- console.log(a); // 1
- // Variables defined by var can be accessed across block scopes.
- ( function A() {
- var b = 2;
- console.log(b); // 2
- })();
- // console.log(b); // report errors,
- // It can be seen that variables defined through var cannot be accessed across function scopes
- if( true) {
- var c = 3;
- }
- console.log(c); // 3
- for( var i = 0; i < 4; i ++) {
- var d = 5;
- };
- console.log(i); // 4 (at the end of the cycle, i is already 4, so i here is 4)
- console.log(d); // 5
- // Variables defined by var in if statement and for statement can be accessed from outside,
- // It can be seen that if statements and for statements belong to block scope, not function scope.
- </script>
2. Differences among VaR, let and const
- Variables defined by var have no block concept and can be accessed across blocks, not functions.
- let defined variables can only be accessed within the block scope, not across blocks or functions.
- const is used to define constants. It must be initialized (that is, it must be assigned) when used. It can only be accessed in the block scope and cannot be modified.
- <script type= "text/javascript">
- // Block scope
- {
- var a = 1;
- let b = 2;
- const c = 3;
- // c = 4; // report errors
- var aa;
- let bb;
- // const cc; // report errors
- console.log(a); // 1
- console.log(b); // 2
- console.log(c); // 3
- console.log(aa); // undefined
- console.log(bb); // undefined
- }
- console.log(a); // 1
- // console.log(b); // report errors
- // console.log(c); // report errors
- // Function scope
- ( function A() {
- var d = 5;
- let e = 6;
- const f = 7;
- console.log(d); // 5
- console.log(e); // 6
- console.log(f); // 7
- })();
- // console.log(d); // report errors
- // console.log(e); // report errors
- // console.log(f); // report errors
- </script>
3. Can object attributes defined by const be changed
This is a question I met during the interview today. It said const can't be modified, so I happily said no, but I found it wrong after the actual test. Record it here.
- const person = {
- name : 'jiuke',
- sex : 'male'
- }
- person.name = 'test'
- console.log(person.name)
Run the above code and find that the name attribute of the person object has indeed been modified. What's going on?
Because the object is of reference type, only the pointer of the object is saved in person, which means that const only ensures that the pointer does not change, and modifying the object's properties will not change the pointer of the object, so it is allowed. In other words, as long as the pointer of the reference type defined by const does not change, other changes are allowed no matter how.
Then we try to modify the pointer and let person point to a new object. Sure enough, an error is reported
- const person = {
- name : 'jiuke',
- sex : 'male'
- }
- person = {
- name : 'test',
- sex : 'male'
- }