Type conversion
Typeof is used to detect the typeof a variable with a value of
typeof null = 'object'
typeof [] = 'object'
typeof NaN = 'number'
typeof 1 = 'number'
typeof ' ' = 'string'
typeof true = 'boolean'
typeof {} = 'object'
typeof function(){} = 'function'
Copy the code
Implicit type conversions generally occur with numeric operations, such as:
+true = 1
+' ' = 0
1 * '22' = 22
1 + '22' = '122' // Non-implicit type conversion
Copy the code
In general, ”, false, undefined, null, NaN, and 0 are judged false in conditional statements
var x = 1;
if (function f(){}) {
x += typeof(f)
}
console.log(x); // the function fin if does not promote, so f type is undefined, x value is 1undefined
Copy the code
Operator priority () The operator has the highest priority
Scope and scope chain
A scope chain is a stack, and the later it is generated, the closer it is to the top of the stack. When a variable is searched, it is searched from the top of the stack, that is, from the current scope until the outermost scope is found or the global scope location is found.
When a function is defined, the inner scope inherits a previously existing scope
function a (){
function b (){
var bbb = 234;
console.log(aaa);
}
var aaa = 123;
return b;
}
var glob = 100;
var demo = a();
demo();
// the scope of the definition of b: a AO and GO
// the scope of b execution: a AO and GO, B AO
// So aaa is 123
Copy the code
So when you create a closure, the scope of the function is the scope of the function definition
Closure definition: A closure is created when an internal function or variable is saved externally
function a() {
var num = 100;
function b() {
num ++;
console.log(num);
}
window.b = b; // This also generates closures;
return b;
}
var demo = a();
demo(); / / 101
demo(); / / 102
demo(); / / 103
demo(); / / 104
demo(); / / 105
// Since b is saved outside the scope of a, b inherits the scope of A, so num is saved outside the scope of B, so num will be +1 every time the returned function is executed
Copy the code
Hazards of closures: The original scope chain will not be released, resulting in memory leaks
Closure functions:
1. Implement common variables
2, caching,
3. Realize encapsulation and privatize attributes
4, modular development, prevent pollution of global variables
function eater() {
var food = "";
var obj = {
eat: function () {
console.log("I am eating " + food);
food = "";
},
push: function (myFood) { food = myFood; }}return obj;
}
var eat = eater();
eat.push("apple")
eat.eat() // I am eating apple
Copy the code
Execute function immediately
A function that executes only once is called an initializer, while a function that executes immediately releases the function to avoid consuming space.
! (function(a1, a2, a3) {
var a = 123;
var b = 234;
console.log(a + b);
console.log(a1, a2, a3); }) (1.2.3)
Copy the code
There are two immediate functions:
(function() {} ());/ / recommend
(function() {}) ();Copy the code
For functions: function expressions execute immediately, but function declarations do not. Function names that can be executed by executing symbols can be ignored. At this point the function can become an immediate function
var test = function() {
console.log('aaa'); } ()// after conversion to
var test = (function(){
console.log('aaa')
}())
Copy the code
So anything that converts a function declaration into an expression makes it an immediate function, such as logical and, or, not, plus, minus, and ().
+function() {
console.log('a'); } ()function() {
console.log('a');
}()
!function() {
console.log('a'); } ()Copy the code
There are several ways in javascript to make a function declaration an expression:
Var a = function(){}();
{a: function(){}()}
Function ({a: function(){}}).a()
4, call expression:! function(){}()
New function(){}
Comma expression
Comma expressions are comma-delimited expressions that always return the last value of the expression, as shown below
Var a = (1, 2, 3), where a is equal to 3