C:\Users\Administrator\Desktop\手写\01_instanceOf.js
| function instance_of(target, origin) { |
| let targetP = target.__proto__; |
| let originP = origin.prototype; |
| while (true) { |
| if (targetP === originP) return true; |
| if (targetP === null) return false; |
| targetP = targetP.__proto__; |
| } |
| } |
| |
| console.log([1] instanceof Array); |
| console.log(instance_of([1], Array)); |
| console.log(instance_of([1], Object)); |
| console.log(instance_of([1], Map)); |
C:\Users\Administrator\Desktop\手写\02_模拟new关键字.js
| let arr = objectFactory(Array, 'cxk', '18'); |
| console.log(arr); |
| |
| function objectFactory() { |
| |
| const obj = new Object(); |
| |
| const Constructor = [].shift.call(arguments); |
| |
| obj.__proto__ = Constructor.prototype; |
| |
| const ret = Constructor.apply(obj, arguments); |
| |
| return typeof ret === 'object' ? ret : obj; |
| } |
C:\Users\Administrator\Desktop\手写\03_继承.js
| function Person(name, age) { |
| this.name = name; |
| this.age = age; |
| } |
| Person.prototype.eating = function () { |
| console.log('I am eating'); |
| }; |
| |
| function Student(name, age, grade) { |
| Person.call(this, name, age); |
| this.grade = grade; |
| } |
| |
| |
| |
| Student.prototype = Object.create(Person.prototype); |
| |
| Student.prototype.constructor = Student; |
| |
| Student.prototype.study = function () { |
| console.log('I am studying'); |
| }; |
| |
| const stu = new Student('Alice', 19, '一年级'); |
| console.log(stu); |
| stu.eating(); |
C:\Users\Administrator\Desktop\手写\04_继承_class关键字.js
| class Person { |
| constructor(name, age) { |
| this.name = name; |
| this.age = age; |
| } |
| eating() { |
| console.log('I am eating'); |
| } |
| } |
| |
| class Student extends Person { |
| constructor(name, age, grade) { |
| super(name, age); |
| this.grade = grade; |
| } |
| studying() { |
| console.log('I am studying'); |
| } |
| } |
| |
| const stu = new Student('Alice', 19, '一年级'); |
| console.log(stu); |
| stu.eating(); |
C:\Users\Administrator\Desktop\手写\05_手写call函数.js
| Function.prototype.myCall = function (context, ...args) { |
| let cxt = context || window; |
| |
| |
| let func = Symbol(); |
| |
| cxt[func] = this; |
| console.log(args); |
| args = args ? args : []; |
| |
| const res = args.length > 0 ? cxt[func](...args) : cxt[func](); |
| |
| delete cxt[func]; |
| |
| return res; |
| }; |
| |
| const obj = { name: 'Alice' }; |
| function foo(value1, value2) { |
| console.log('this is foo ↓↓↓↓↓' + value1 + value2); |
| console.log(this); |
| return '我是返回值'; |
| } |
| foo.myCall(obj, 'hello hahahahahaha', 'heiheihei'); |
C:\Users\Administrator\Desktop\手写\06_手写apply方法.js
| Function.prototype.myApply = function (context = window, args = []) { |
| let sy = Symbol(); |
| context[sy] = this; |
| let res = args.length > 0 ? context[sy](...args) : context[sy](); |
| return res; |
| }; |
| |
| const obj = { name: 'Alice' }; |
| function foo(value1, value2) { |
| console.log('this is foo ↓↓↓↓↓' + value1, value2); |
| console.log(this); |
| return '我是返回值'; |
| } |
| const value = foo.myApply(obj, ['hello hahahahahaha', 'heiheihei']); |
| console.log(value); |
| |
| |
C:\Users\Administrator\Desktop\手写\07_手写双向绑定vue2.js
C:\Users\Administrator\Desktop\手写\08_手写数据双向绑定vue3.js
| let obj = { |
| name: '55', |
| }; |
| proxyObj = new Proxy(obj, { |
| get: function (target, value) { |
| console.log('读取了数据'); |
| return target[value]; |
| }, |
| set: function (target, value, newValue) { |
| target[value] = newValue; |
| console.log('设置了数据'); |
| }, |
| }); |
| |
| proxyObj.name; |
| proxyObj.name = 'alice'; |
C:\Users\Administrator\Desktop\手写\09_防抖.js
| function deBounce(fn, delay) { |
| let timer = null; |
| return function () { |
| clearTimeout(timer); |
| timer = setTimeout(() => { |
| fn.apply(this, arguments); |
| }, delay); |
| }; |
| } |
C:\Users\Administrator\Desktop\手写\10_深拷贝.js
| function isObject(value) { |
| const valueType = typeof value; |
| return value !== null && (valueType === 'function' || valueType === 'object'); |
| } |
| function deepCopy(obj, map = new WeakMap()) { |
| const newObj = obj instanceof 'Array' ? [] : {}; |
| if (!isObject(obj)) return obj; |
| if (map.has(obj)) return map.get(obj); |
| map.set(obj, newObj); |
| for (let key of obj) { |
| newObj[key] = deepCopy(obj[key], map); |
| } |
| } |
C:\Users\Administrator\Desktop\手写\11_二叉树的遍历.js
| |
| |
| |
| function dfsPreorderByRcs(tree) { |
| const output = []; |
| |
| const visitLoop = (node) => { |
| if (node) { |
| |
| output.push(node.data); |
| |
| visitLoop(node.left); |
| |
| visitLoop(node.right); |
| } |
| }; |
| |
| visitLoop(tree); |
| return output; |
| } |
| |
| console.log('递归法DFS(前序): ', dfsPreorderByRcs(tree)); |
| |
C:\Users\Administrator\Desktop\手写\12_生成器模拟async和await.js
| function requestData() { |
| return new Promise((resolve, reject) => { |
| resolve('11110000'); |
| }); |
| } |
| |
| function* request() { |
| const data = yield requestData(); |
| console.log('1__' + data); |
| const data2 = yield requestData(); |
| console.log('2__' + data2); |
| return 'ending'; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function execGen(genFn) { |
| const gen = genFn(); |
| |
| const exec = (res) => { |
| let genData = gen.next(res); |
| if (genData.done) return genData.value; |
| |
| genData.value.then((res) => { |
| exec(res); |
| }); |
| }; |
| exec(); |
| } |
| |
| execGen(request); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!