Cxwht's Blog - 默认分类
type: channel
link: https://www.cxwht.com/category/default
description: 只是一个默认分类
items: ECMA
items: JS 手写

ECMA
type: item
date: 2022-01-17T10:22:22+08:00
description: 1. 规范ES5英文版ES5中文版ES6英文版2. 进入全局代码执行全局代码2.1 1.global.jssrc1.global.jsvar a = 1; function one(c) { var b = 2; console.log(a, b, c); } one(3);2.2 1.global.run.jssrc1.global.run.js/** var a = 1; function one(c) { var b = 2; console.log(a, b, c); } one(3); */ const LexicalEnvironment = require('./LexicalEnvironment'); const ExecutionContext = require('./ExecutionContext'); const ECStack = require('./ECStack'); //1.将变量环境设置为全局环境 2.将词法环境设置为 全局环境 const globalLexicalEnvironment = LexicalEnvironment.NewObjectEnvironment(global, null); const globalEC = new ExecutionContext(globalLexicalEnvironment, global); ECStack.push(globalEC); //令 env 为当前运行的执行环境的环境变量的 环境记录 let env = ECStack.current.lexicalEnvironment.environmentRecord; //如果 code 是 eval 代码 ，则令 configurableBindings 为 true，否则令 configurableBindings 为 false。 let configurableBindings = false; //如果代码是 严格模式下的代码 ，则令 strict 为 true，否则令 strict 为 false。 let strict = false; //按源码顺序遍历 code，对于每一个 VariableDeclaration 和 VariableDeclarationNoIn 表达式： //令 dn 为 d 中的标识符。 let dn = 'a'; //以 dn 为参数，调用 env 的 HasBinding 具体方法，并令 varAlreadyDeclared 为调用的结果 let varAlreadyDeclared = env.HasBinding(dn); //如果 varAlreadyDeclared 为 false，则： if (!varAlreadyDeclared) { //以 dn 和 configurableBindings 为参数，调用 env 的 CreateMutableBinding 具体方法。 env.CreateMutableBinding(dn, configurableBindings); //以 dn、undefined 和 strict 为参数，调用 env 的 SetMutableBinding 具体方法。 env.SetMutableBinding(dn, undefined, strict); } console.log(env.GetBindingValue('a'));2.3 LexicalEnvironment.jssrcLexicalEnvironment.jsconst DeclarativeEnvironmentRecords = require("./DeclarativeEnvironmentRecords"); const ObjectEnvironmentRecords = require("./ObjectEnvironmentRecords"); class LexicalEnvironment { /** * 当调用 GetIdentifierReference 抽象运算时 * @param {*} lex 词法环境 * @param {*} name 标识符字符串 * @param {*} strict 布尔型标识 */ static GetIdentifierReference(lex, name, strict) { //if (!lex) return { name: undefined }; if (!lex) throw new Error(`ReferenceError: ${name} is not defined`); let envRec = lex.environmentRecor…

JS 手写
type: item
date: 2022-01-17T10:21:01+08:00
description: 1.call的实现Function.prototype.call = function(context,...args){ let cxt = context || window; let func = Symbol() cxt[func] = this; args = args ? args : [] const res = args.length > 0 ? cxt[func](...args) : cxt[func](); delete cxt[func]; return res; }2. apply的实现Function.prototype.apply = function(context,args = []){ let cxt = context || window; let func = Symbol() cxt[func] = this; const res = args.length > 0 ? cxt[func](...args) : cxt[func](); delete cxt[func]; return res; }3. bind的实现Function.prototype.bind = function (context, ...args) { const fn = this args = args ? args : [] return function newFn(...newFnArgs) { if (this instanceof newFn) { return new fn(...args, ...newFnArgs) } return fn.apply(context, [...args,...newFnArgs]) } }4. 寄生式组合继承function Person(obj) { this.name = obj.name this.age = obj.age } Person.prototype.add = function(value){ console.log(value) } var p1 = new Person({name:"zhangsan", age: 18}) function Person1(obj) { Person.call(this, obj) this.sex = obj.sex } Person1.prototype = Object.create(Person.prototype); Person1.prototype.constructor = Person1; Person1.prototype.play = function(value){ console.log(value) } var p2 = new Person1({name:"lisi", age: 18, sex: "男"}) 5. ES6继承class Person{ constructor(name='zhangsan',age=18){ this.name = name; this.age = age; } desc(){ console.log(`${this.name} ${this.age}`) } } class Man extends Person{ constructor(name = 'lisi',age = 18){ super(name, age); } desc(){ super.eat() } }6. new的实现function new(ctor,...args){ if(typeof ctor !== 'function'){ throw 'the first param must be a function'; } var newObj = Object.create(ctor.prototype); var ctorReturnResult = ctor.apply(newObj, args); var isObject = typeof ctorReturnResult === 'object' && ctorReturnResult !== null; var isFunction = typeof ctorReturnResult === 'function'; if(isObject || isFunction){ return ctorReturnResult; } return newObj; } let c = new(Ctor);7.instanceof的实现function myInstanceOf(a,b){ let left = a.__proto__; let right = b.prototype; while(true){ …