Home  >  Article  >  Web Front-end  >  An in-depth chat about JavaScript

An in-depth chat about JavaScript

WBOY
WBOYforward
2022-06-16 11:53:122087browse

This article brings you relevant knowledge about javascript, which mainly includes why the JavaScript engine is needed, the relationship between the browser kernel and the js engine, environment variables and records, etc., as follows Let's take a look, I hope it will be helpful to everyone.

An in-depth chat about JavaScript

[Related recommendations: javascript video tutorial, web front-end

TypeScript will replace JavaScript?

  1. TypeScript just brings type thinking
    Because JavaScript itself has no restrictions on variables, function parameters and other types for a long time
    This may bring some security issues to our project Hidden danger
  2. A series of type constraint schemes appeared in the JavaScript community
    In 2014, Facebook launched flow to perform type checking on JavaScript

3.Type source To JavaScript, to JavaScript

Why do we need a JavaScript engine

High-level programming languages ​​need to be converted into final machine instructions for execution
In fact, no matter what JavaScript we write, Leave it to the browser or Node for execution, and finally it needs to be executed by the CPU
So we need the JavaScript engine to help us translate the JavaScript code into CPU instructions for execution

The relationship between the browser core and the JS engine

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: parse and execute JavaScript code

Variable environment and recording

VO (Variable Object) variable object In the latest ECMA standard, VO already has another The variable environment VE

GO (Clobal Object) is called the global object, and the global execution context

AO (Activation Objece) includes the function execution context

memory management and Closure

  1. Understanding memory management

An in-depth chat about JavaScript

##Memory management of JS

JavaScript will allocate memory for us when defining variables JS’s allocation of basic data type memory will be allocated directly in the stack space during execution;
JS’s allocation of complex data type memory Allocation will open up a space in the heap memory, and refer to the pointer return value variable of this space

Garbage collection of JS

Because

the size of the memory It is limited, so when the memory is no longer needed, we need to release it to free up more memory space

Garbage collection in English is

Garbage Collection GC for short For objects that are no longer used, we call them garbage, and they need to be recycled to release more memory space
And our language running environment, such as the java running environment JVM , JavaScript's running environment js engine will have a memory garbage collector
We also refer to the garbage collector as GC, so in many places you see that GC actually refers to the garbage collector

In-depth closure

Definition of closure in computer science (Wikipedia):

Closure (English: Closure), also known as lexical closure (Lexical Closure) or function closure (function closures) );
is a technology for implementing 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 even if it is separated from the context at the time of capture , it can also run as usual

The concept of closures appeared in the 1960s, and the earliest program to implement closures was Scheme, so we can understand why there are closures in JavaScript;

Because there are a lot of closures in JavaScript The design is derived from Scheme;

An in-depth chat about JavaScript

Let’s take a look at MDN’s explanation of JavaScript closures:

A function and its surrounding state (lexical environment, lexical environment) References are bundled together (or functions are surrounded by references), and this combination is a closure
In other words, a closure allows you to access the scope of its outer function in an inner 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, if it can access the outer layer that it acts on Free variable, then this function is a closure;
From a broad perspective: functions in JavaScript are closures;
From a narrow perspective: a function in JavaScript, if it accesses the outer layer Acts on the variable, then it is a closure;

this指向

在全局作用域下:
浏览器:window
node环境:{}

箭头函数 arrow function

箭头函数是ES6 之后增加的一种编写函数的方法,并且它比函数表达式更加简洁;
箭头函数不会绑定this、arguments属性;
箭头函数不能作为构造函数来使用(不能和new一起来使用,会抛出错误)

认识arguments

arguments是一个对应于 传递给函数的参数的类(伪)数组(array-like) 对象

理解JvaScript纯函数

函数式编程中有一个非常重要的概念叫做纯函数,JavaScript符合函数式编程的规范,所以也有纯函数的概念;

纯函数的维基百科定义:
在程序设计中,若一个函数符合以下条件,那么这个函数辈称为纯函数
此函数在相同的输入值时,需要产生相同的输出
函数的输出和输入值以外的其他隐藏信息或状态无关,也和由I/O设备产生的外部输出无关
改函数不能有语义上可观察的函数副作用,诸如 “触发事件”,使输出设备输出,或更改输出值以外物件的内容等
总结:
确定的输入,一定产生确定的输出;
函数在执行过程中,不能产生副作用;

副作用:

JavaScript 柯里化

柯里化也是属于函数式编程里面一个非常重要的概念
维基百科解释:
在计算机科学中,柯里化(Currying) ,又译为卡瑞化 或加里化
是八接收多个参数的函数,变成接收一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数,而且返回结果的新函数
柯里化声称:如果你固定某些参数,你将得到接受余下参数的一个函数

总结:
只传递给函数一部分参数来调用它,让它返回一个函数区处理剩余的参数;
这个过程就称为柯里化

为什么需要柯里化:
在函数式编程中,我们其实往往希望一个函数处理的问题尽可能的单一,而不是将一大堆的处理过程交给一个函数来处理

function foo(x,y,c) {
    return x + y + c
}
console.log(foo(10,20,30))

//柯里化
function sum(x) {
    return function(y) {
        return function(z) {
            return x + y + z
        }
    }
}
var a = sum(10)(20)(30)
console.log(a )

//简化柯里化
var sum2 = x => y => z => {
    return x + y + z
}
console.log(sum2(10)(20)(30 ))

组合函数

组合函数(Compose) 函数是在JavaScript开发中一种对函数的使用技巧、模式:
比如我们现在需要对某个数据进行函数的调用,执行两个函数fn1 和 fn2,这两个函数是依次执行的
那么如果我们每次都需要进行两个函数的调用,操作上就会显示的重复
那么是否可以将这两个函数组合起来,自动依次调用呢?
这个过程就是对函数的组合,我们称之为组合函数(Compose Function)

其他内容

with语句

with 语句
+作用: 可以形成自己的作用域
不建议使用with语句 ,因为它可能是混淆错误和兼容性问题的根源

var obj2 = {name:'Tom',age:18,message:'obj2'}

// var message = "hello world"

function foo() {
function bar () {
     with(obj2) {
          console.log(message)
     }
}
bar()
}
foo()
eval函数

eval是一个特殊的函数,它可以将传入的字符串当作JavaScript 代码来运行

   var strFn = 'var message = "Hello world"; console.log(message);';
   eval(strFn)

不建议在开发中使用eval:
eval代码的可读性非常的差(代码的可读性是高质量代码的重要原则);
eval是一个字符串,那么有可能在执行的过程中辈可以篡改,那么可能会造成被攻击的风险;
eval的执行必须经过JS解释器,不能不被JS引擎优化;

严格模式 strict Mode

 严格模式是一种具有限制性的JavaScript模式,从而使代码隐式的脱离了"懒散(sloppy) 模式"
 支持严格模式的浏览器在监测到代码中有严格模式时,会以更加严格的方式对代码进行监测和执行

 严格模式通过抛出错误来消除一些原有的静默(silent)错误
 严格模式让Js引擎周期执行代码时可以进行更多的优化(不需要对一些特殊的语法进行处理)
"use strict"; // 开启严格模式var message = "hello world"console.log(message)

严格模式限制
这里我们来说几个严格模式下的严格语法限制:
JavaScript被设计为新手开发者更容易上手,所以有时候本来错误语法,被认为也是可以正常被解析的
但是在严格模式下,这种失误会被当成错误,以便可以快速的发现和修正

  1. 无法意外的创建全局变量
// 1. 意外创建全局变量
    message = "Hello world"
    console.log(message)

    function foo() {
        age = 20
    }
    foo()
    console.log(age)
  1. 严格模式会时引起静默失败(silently fail ,注:不报错也没有任何效果)的赋值操作抛出异常
//默认静态错误
true.name ='xiaoluo';
NaN = 123
  1. 严格模式下试图删除不可删除的属性
  2. 严格模式不允许函数参数有相同的名称
// 不允许函数参数有相同的名称function foo(x,y,x) {
    console.log(x,y,x)}foo(10,20,30)
  1. 不允许0 的八进制语法
var num = 0o123 // 八进制
var num2 = 0x123 // 十六进制
console.log(num,num2)
  1. 在严格模式下, 不允许使用with
var obj2 = {name:'Tom',age:18,message:'obj2'}

with(obj2) {
      console.log(message)
     }
  1. 在严格模式下,eval 不再为上层引用变量
var strFn = 'var message = "Hello world"; console.log(message);';
eval(strFn)
console.log(message)
  1. 严格模式下,this绑定不会默认转成对象
    严格模式下,自执行函数会指向undefined
function foo() {
    console.log(this) //undefined
}
foo()

【相关推荐:javascript视频教程web前端

The above is the detailed content of An in-depth chat about JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete