Go 自定义 error 断言 发表于 2018-11-02 | 分类于 Go | 评论数: 本文字数: 522 | 阅读时长 ≈ 1 分钟 error 是一个 interface 123type error interface { Error() string} 阅读全文 »
inherits 实现 发表于 2017-08-23 | 分类于 前端 | 评论数: 本文字数: 1.1k | 阅读时长 ≈ 1 分钟 inherits 实现12345678910111213141516171819202122232425262728293031if (typeof Object.create === 'function') { // 1. 判断是否具备Object.create方法 // 2. 定义inherits方法 function inherits(ctor, superCtor) { ctor.super = superCtor; // 3. 设置“子类”构造函数原型对象 // Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__ // 通过Object.create()方法设置“子类”构造函数原型对象的__proto__为“父类”构造函数原型 // 另外将“子类”构造函数原型对象的constructor设回为“子类”构造函数 ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, enumerable: false, writable: true, configurable: true } }); }} else { // 4. 若不支持Obeject.create()方法 function inherits(ctor, superCtor) { ctor.super = superCtor; // 5. 创建一个临时空构造函数 var tempCtor = function() {}; // 6. 使“父类”构造函数prototype对象寄生到临时空构造函数 tempCtor.prototype = superCtor.prototype; // 7. 使“子类”构造函数prototype对象为临时构造函数实例, ctor.prototype = new tempCtor(); ctor.prototype.constructor = ctor; }} 阅读全文 »
call、apply、bind 模拟实现 发表于 2017-06-12 | 分类于 前端 | 评论数: 本文字数: 1.1k | 阅读时长 ≈ 1 分钟 call 方法模拟实现123456789101112Function.prototype.call = function(context, ...args) { var context; try { context = context || window; } catch (e) { context = global; } context.fn = this; const result = context.fn(...args); delete context.fn; return result;}; 阅读全文 »
浏览器渲染流程 发表于 2017-04-13 | 分类于 前端 | 评论数: 本文字数: 1.6k | 阅读时长 ≈ 1 分钟 浏览器输入 URL 至页面最后呈现的过程 用户输入 URL 地址 浏览器解析出主机名 通过查询 DNS 转换为服务器 IP 地址( 按浏览器、本地操作系统、本地 host 文件、路由器、ISP(服务提供商)DNS 缓存、顶级 / 根 DNS 服务器 逐级查询 DNS 缓存) 传输层:将 IP 地址端口号解析出来,并与目标 Web 服务器建立 TCP 连接(3 次握手) 应用层:发送 http 请求,设置好请求行(方法、路径、协议版本)、请求头、空行、请求主体发送请求报文,服务器接收请求后,根据处理结果将响应报文( 响应行、 响应头、 空行、响应主体)返回相关资源给浏览器 资源请求完毕,关闭连接(4 次挥手),浏览器开始渲染页面 阅读全文 »