3.Type originates from JavaScript and belongs to JavaScript.
Advanced programming languages need to be converted into final machine instructions to execute. In fact, no matter whether the JavaScript we write is handed over to the browser Or Node execution, in the end it needs to be executed by the CPU, so we need the JavaScript engine to help us translate the JavaScript code into CPU instructions to execute
Here we take WebKit as the column. WebKit is actually composed of two parts. :
WebCore: Responsible for HTML parsing, layout, rendering and other related work
JavaScriptCore: parsing and executing JavaScript code
VO (Variable Object) variable object. In the latest ECMA standard, VO already has another name for the variable environment VE
GO (Clobal Object) global object and global execution context
AO (Activation Objece). ) includes function execution context
JavaScript will allocate memory for us when defining variables
JS allocates memory for basic data types directly in the stack space during execution;
JS's allocation of memory for complex data types will open up a space in the heap memory, and refer to the pointer return value variable of this space.
because the size of the memory is limited , so when the memory is no longer needed, we It needs to be released in order to free up more memory space.
Garbage collection in English is Garbage Collection or GC.
For objects that are no longer used, we call them garbage. They need to be recycled to release more memory space. Our language runtime environment, such as the Java runtime environment JVM, and the JavaScript runtime environment js engine, will Memory garbage collector, garbage collector, we also call it GC for short, so in many places you see that GC actually refers to the garbage collector.
is the definition of closure in computer science (Wikipedia):
Closure (English: Closure), also known as lexical closure (Lexical Closure) or function closure (function closures);
It is a technology that implements lexical binding in programming languages that support first-class functions;
In implementation, closure is a structure that stores a function and an associated environment (equivalent to a symbol lookup table);
The biggest difference between a closure and a function is that when a closure is captured, its free variables will be determined at the time of capture, so that it can run as usual even if it is out of context at the time of capture
. The concept of closure appeared in the 1960s. The earliest program to implement closures was Scheme, so we can understand why there are closures in JavaScript;
Because a lot of designs in JavaScript are derived from Scheme;
Let’s take a look at MDN’s explanation of JavaScript closures:
A function is bundled with a reference to its surrounding state (lexical environment) (or the function is surrounded by references). This combination is a closure. In other words, a closure allows you to access it within an inner function. to the scope of its outer function;
In JavaScript, whenever a function is created, the closure will be created at the same time as the function is created;
function foo() { var name = 'why' var age = 18 function bar() { console.log('bar ',name) } return bar}var fun = foo()fun()
Summary:
An ordinary function function is a closure if it can access the free variables that the outer layer acts on;
From a broad perspective: functions in JavaScript are closures;
From a narrow point of view: if a function in JavaScript accesses an outer variable, it is a closure;
to the global scope:
Browser: window
Node environment: {}
Arrow function is a method of writing functions added after ES6, and it is more concise than function expressions;
Arrow functions will not bind this and arguments attributes;
Arrow functions cannot be used as constructors (cannot be used with new, an error will be thrown).
arguments is a (pseudo) array (array-like) corresponding to the parameters passed to the function. Object
functions There is a very important concept in functional programming called pure functions. JavaScript conforms to the specifications of functional programming, so there is also the concept of pure functions;
Wikipedia definition of pure functions:
In programming, if a function meets the following conditions, then this function is called a pure function. When the function has the same input value, it needs to produce the same output. The output of the function has nothing to do with other hidden information or states other than the input value. Functions unrelated to external output generated by I/O devices cannot have semantically observable function side effects, such as "triggering events", causing output devices to output, or changing the contents of objects other than output values. Summary:
Determined input must produce certain output;
During the execution of a function, no side effects can occur;
side effects:
Currying is also a very important concept in functional programming Wikipedia explains:
In computer science, Currying (Currying), also translated as Currying or Currying, is a function that accepts multiple parameters, becomes a function that receives a single parameter (the first parameter of the original function), and returns A new function that accepts the remaining arguments and returns a result Curried claim: If you fix certain arguments, you will get a function
summary that accepts the remaining arguments:
Pass only part of the parameters to the function to call it, and let it return a function area to process the remaining parameters;
This process is called currying.
Why currying is needed:
In functional programming, we often hope that the problem handled by a function is as simple as possible, instead of handing over a lot of processing to a function to handle
function foo(x,y,c) { return x + y + c } console.log(foo(10,20,30)) //Curry function sum(x) { return function(y) { return function(z) { return x + y + z } } } var a = sum(10)(20)(30) console.log(a) //Simplify currying var sum2 = x => y => z => { return x + y + z } console.log(sum2(10)(20)(30))
function (Compose) function is a technique and mode for using functions in JavaScript development:
For example, we now need to call a function on a certain data and execute two functions fn1 and fn2. These two functions are executed in sequence. If we need to call two functions every time, the operation will appear repetitive. So is it possible to combine these two functions and call them one after another automatically?
This process is the combination of functions, which we call Compose Function.
with statement
+Function: You can form your own scope. It is not recommended to use the with statement because it may be the source of confusion errors and compatibility issues.
var obj2 = {name:'Tom',age:18,message:'obj2'} // var message = "hello world" function foo() { function bar () { with(obj2) { console.log(message) } } bar() } foo()
eval is a special function that can run the incoming string as JavaScript code
var strFn = 'var message = "Hello world"; console.log(message);'; eval(strFn)
It is not recommended to use eval in development:
The readability of eval code is very poor (code readability is an important principle of high-quality code);
eval is a string, so it may be tampered with during execution, which may cause the risk of being attacked;
The execution of eval must go through the JS interpreter and must be optimized by the JS engine;
mode is a restrictive JavaScript mode, which makes the code implicitly break away from the "sloppy mode" When browsers that support strict mode detect strict mode in the code, they will monitor and execute the code in a more strict manner. Strict mode will eliminate some original silent errors by throwing errors. Strict mode allows the Js engine to More optimizations can be performed when executing code periodically (no need to deal with some special syntax)
"use strict"; // Turn on strict mode var message = "hello world" console.log(message)
strict mode restrictions <br/ > Here we talk about some strict syntax restrictions in strict mode:
JavaScript is designed to be easier for novice developers to get started, so sometimes incorrect syntax is considered to be parsed normally. However, in strict mode, such mistakes will be treated as errors so that
// 1. Accidentally create a global variable message = "Hello world" console.log(message) function foo() { age=20 } foo() Console.log(age)
//Default static error true.name ='xiaoluo'; NaN = 123
// Function parameters are not allowed to have the same name function foo(x,y,x) { console.log(x,y,x)}foo(10,20,30)
var num = 0o123 // Octal var num2 = 0x123 // Hexadecimal console.log(num,num2)
var obj2 = {name:'Tom',age:18,message:'obj2'}is not allowed
with(obj2) { console.log(message) }
var for the upper layer strFn = 'var message = "Hello world"; console.log(message);'; eval(strFn)In
console.log(message)
function foo() { console.log(this) //undefined } foo()