
JS
aeipyuan
渣渣一枚,请多指教。。。。。。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
优化js定时器准确性
思路:使用时间戳,计算延迟时间,用延迟时间抵消一部分定时器的时长不做优化let cnt = 0;let start = Date.now();// 耗时任务setInterval(() => { let num = 100000000; while (num--) { }});setInterval(() => { cnt++; console.log("延迟:" + (Date.now() - (start + cnt * 1000)));}, 1000);原创 2020-08-03 21:39:48 · 1012 阅读 · 0 评论 -
几种js设计模式
1. 工厂模式作用:实现对象的批量创建/* 构造函数 */function Person(name) { this.name = name;}function Car(model) { this.model = model;}/* 创建 */function create(type, val) { return (this instanceof create) ? this[type](val) : new create(type, val);}create.pro.原创 2020-08-01 09:21:10 · 447 阅读 · 0 评论 -
JS链表实现栈和队列
1. 栈top指针记录栈顶元素,插入和删除都是对top操作function Node(val) { this.val = val; this.next = null;}function Stack() { this.top = null; this.length = 0;}/* 入栈 */Stack.prototype.push = function (node) { if (!this.top) { this.top = node;原创 2020-07-06 19:10:23 · 447 阅读 · 0 评论 -
js数组扁平化的几种实现方式
1. 递归调用let res = [];function flattern(arr) { arr.map(item => { if (Array.isArray(item)) res.concat(flattern(item)); else res.push(item); })}2. reduce结合递归function flattern(arr) { return arr.reduce原创 2020-07-06 08:54:16 · 355 阅读 · 1 评论 -
toUpperCase和toLowerCase的实现
实现过程(以小写转大写为例):将字符串拆开为一个一个字符使用charCodeAt获取每个字符的ASCII码使用String.fromCharCode将ASCII码-32的结果转化为字符合并转换后的数组function toUpperCase(str) { let arr = str.split(''); for (let i = 0; i < arr.length; i++) { if (arr[i] >= 'a' && arr[i]原创 2020-05-22 08:28:30 · 341 阅读 · 0 评论 -
new 和 Object.create()区别
new 和 Object.create()区别new操作符创建一个对象的过程创建一个对象obj将obj连接到原型链上,即设置obj.__proto__ = Constructor.prototype绑定this指向,传参执行原型函数(参数应用到obj对象上)判断执行结果,没有则返回obj/* 手写方法 */function myNew() { /* 创建新对象 */ let obj = new Object(); /* 构造函数,取第一个传入的参数,argument原创 2020-05-16 19:35:53 · 271 阅读 · 0 评论 -
实现一个简易版的react
实现一个简易版的reactReact.createElement作用:将babel解析的结果转换成树形结构class Element { constructor(type, props) { this.type = type; this.props = props; }}function createElement(type, props, ...children) { props = props || {}; props.chil原创 2020-05-12 13:46:56 · 213 阅读 · 0 评论 -
浅学virtualDom和diff算法
浅学virtualDom和diff算法virtual Dom创建virtual Dom/* 实现 */class Element { constructor(type, props, children) { this.type = type; this.props = props; this.children = children; }}function createElement(type, props, children) {原创 2020-05-12 10:18:49 · 247 阅读 · 0 评论 -
express极简实现
简单实现expresslet http = require('http');let url = require('url');let createApplication = () => { let app = (req, res) => { /* 获取路径和方法 */ let { pathname } = url.parse(req.url...原创 2020-04-25 18:02:41 · 147 阅读 · 0 评论 -
Vuex简单实现
Vuex简单实现1. 实现this.$store全局访问const install = (vue) => { Vue = vue; Vue.mixin({ beforeCreate() { /* 获取根组件传递$store */ if (this.$options && this.$optio...原创 2020-04-22 18:23:10 · 253 阅读 · 0 评论 -
js经典题目学习总结
js经典题目学习总结1.foo与getNamefunction foo() { getName = function () { console.log(1); } return this;}foo.getName = function () { console.log(2);}foo.prototype.getName = functio...原创 2020-04-21 11:37:29 · 306 阅读 · 0 评论 -
new方法的实现 javascript
new方法的实现 javascript在js中,new一个对象的过程中分为以下几个步骤创建一个Object对象obj获取传入的构造函数形成原型链,将构造函数原型与obj链接执行构造函数,绑定this指向为obj判断构造函数返回值类型,若不是对象类型,返回obj实现代码如下:/* Person类 */function Person(name, age) { this.n...原创 2020-04-21 10:18:05 · 158 阅读 · 0 评论 -
leetCode--n数之和--哈希表/双指针
n数之和两数之和给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。不能重复利用这个数组中同样的元素。题目链接:https://leetcode-cn.com/problems/two-sum/示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2...原创 2020-04-14 23:17:53 · 246 阅读 · 0 评论 -
vue-router简单实现
1.构造VueRouter类constructor(options) { /* 初始化 */ this.mode = options.mode || 'hash'; this.routes = options.routes || []; /* 转化为{'/home':Home}的形式 */ this.routesMap = this.createMap(this.routes); /...原创 2020-04-13 16:20:26 · 226 阅读 · 1 评论 -
Promise的简单实现
什么是Promise?Promise 是异步编程的一种解决方案:从语法上讲,promise是一个对象,从它可以获取异步操作的消息;从本意上讲,它是承诺,承诺它过一段时间会给你一个结果。promise有三种状态:pending(等待态),fulfiled(成功态),rejected(失败态);状态一旦改变,就不会再变。创造promise实例后,它会立即执行。Promise的实现构建Pro...原创 2020-04-11 23:40:08 · 222 阅读 · 0 评论