Lyzhou的博客

  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 搜索

Go自定义error断言

发表于 2018-11-02 | 分类于 Go | 评论数:
本文字数: 522 | 阅读时长 ≈ 1 分钟

error是一个interface

1
2
3
type error interface {
Error() string
}
阅读全文 »

Go的单例模式

发表于 2018-10-25 | 分类于 Go | 评论数:
本文字数: 371 | 阅读时长 ≈ 1 分钟

 保证一个 struct 只有一个实例,并提供一个全局访问点

阅读全文 »

inherits实现

发表于 2017-08-23 | 分类于 前端 | 评论数:
本文字数: 1.1k | 阅读时长 ≈ 1 分钟

inherits实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
if (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 方法模拟实现

1
2
3
4
5
6
7
8
9
10
11
12
Function.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 至页面最后呈现的过程

  1. 用户输入 URL 地址
  2. 浏览器解析出主机名
  3.  通过查询 DNS 转换为服务器 IP 地址( 按浏览器、本地操作系统、本地 host 文件、路由器、ISP(服务提供商)DNS 缓存、顶级/根 DNS 服务器  逐级查询 DNS 缓存)
    dns-query
  4. 传输层:将 IP 地址端口号解析出来,并与目标 Web 服务器建立 TCP 连接(3 次握手)
  5. 应用层:发送 http 请求,设置好请求行(方法、路径、协议版本)、请求头、空行、请求主体发送请求报文,服务器接收请求后,根据处理结果将响应报文( 响应行、 响应头、 空行、响应主体)返回相关资源给浏览器
  6. 资源请求完毕,关闭连接(4 次挥手),浏览器开始渲染页面
阅读全文 »
Lyzhou

Lyzhou

专业实现各种技术Hello, Wolrd!

5 日志
2 分类
4 标签
RSS
GitHub E-Mail
Creative Commons
0%
© 2016 – 2019 Lyzhou | 站点总字数: 5k | 站点阅读时长 ≈ 4 分钟