During the learning process, the places you have been swallowed up in the past will always appear again and again. The following will record the knowledge points you learned today for easy viewing.
Data types in JavaScript
Simple (basic) data types: Number, String, Boolean, Undefined, Null
Complex (reference) data types: Object, Array, Date, function, etc.
The following is a description of the difference between simple (basic) data types and complex (cited) data types:
Simple data type: directly store values in the stack, as shown in the figure below
Complex data type: store references in the stack, as shown in the figure below
After understanding the storage methods of the above two data types, you can distinguish the differences between the two. You can do the following exercises:
var a =10; var b = a; //Question: When changing the value of a, does the value of b change a=20; console.log(b); // 10
var s1 = new Object();var s2 = s1;//Q: After changing the attribute of s1, will the same attribute of s2 change s1.name = "mh";console.log(s2.name); //mh
function f2(arr) { arr = [9,8,7,6,5];//Generate new object arr[0]=-100; } var array = [1,2,4,7,5]; f2(array); console.log(array[0]);// 1Variable promotion, function declaration and variable scope in JavaScript
First, let’s look at the following interview questions:
var num = 10; fun(); function fun() { console.log(num); var num =20; }Before I learn, I will directly answer 10. Now, learning the precompiled concept in JavaScript, I know the concept of improving the var keyword and the declaration concept of function. I know that the above code is equivalent to the following code:
var num;//Global scope encounters var and function enhancement function fun() { var num ; // Local user encounters var console.log(num); num =20; } num = 10; fun();Let’s look at the following question:
//Question: Why does the following error appear? //Uncaught TypeError:fnName is not a function console.log(fnName(1,2)); var fnName = function (a,b) { return a + b; }The above code is equivalent to the following code:
var fnName; console.log(fnName(1,2)); fnName = function (a,b) { return a + b; }You can see that because the function is on the right side of the equal sign, only the var will be raised, and an error of "Uncaught TypeError:fnName is not a function" will be reported.
To understand the scope of variables, you can see the following code:
f1(); console.log(c); console.log(b); console.log(a); function f1() { var a = b = c = 20; console.log(c); console.log(b); console.log(a); }For var a=b=c=20;, such continuous assignment, only a will be declared in the local scope, while the scopes of b and c are global scopes. Therefore, only a in the global scope reports an error as "Uncaught ReferenceError: a is not defined".
The above basic JS essay (a must-read article for rookies) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.