原来博客地址不在维护,新的东西直接发在这里
博客列表
- 【译】缓存 React 中事件监听来提升性能
- 【译】在 React Hooks 中使用 useReducer 的几种用例
- 【译】更好地设计 React 组件
- 【译】精挑细选的一份 React 性能问题优化的清单
- 【译】2019 年 JavaScript 开发者应该都在用的 9 个棒的技巧
- 从一个 bug 探究 React 里面 state 的更新机制
- 【译】用 Chrome 开发者工具以及 React 16 版本分析性能
博客列表
// 正则匹配练习
/*
\ 转义字符
* 匹配前一个表达式0次或多次。等价于 {0,}。
+ 匹配前面一个表达式1次或者多次。等价于 {1,}。
? 匹配0次或者1次,{0,1}
. 匹配除换行符之外的任何单个字符
(x) 匹配 'x' 并且记住匹配项,常用于replace
x(?=y) 匹配'x'仅仅当'x'后面跟着'y'.这种叫做正向肯定查找
x(?!y) 匹配'x'仅仅当'x'后面不跟着'y',这个叫做正向否定查找。
[xyz] 匹配方括号中的任意字符
*/
// 给定这样一个连字符串,写一个function转换为驼峰命名法形式的字符串 getElementById
let s1: string = "get-element-by-id";
function f1(s: string): string {
return s.replace(/-\w/g, (x: string) => {
return x.slice(1).toUpperCase();
})
}
console.log(f1(s1))
// 判断字符串是否包含数字
let s2: string = '12saad';
function f2(s: string): boolean {
const regx = /\d/;
return regx.test(s)
}
console.log(f2(s2))
// 判断电话号码
let s3: string = '14660731152';
function f3(s: string): boolean {
const regx = /^1[34578]\d{9}$/;
return regx.test(s)
}
console.log('3', f3(s3));
/*
给定字符串str,检查其是否符合如下格式
1.XXX-XXX-XXXX
2.其中X为Number类型
*/
let s4: string = '123-234-3213';
function f4(s: string): boolean {
const regx = /^(\d{3}-){2}\d{4}$/;
return regx.test(s);
}
console.log('4', f4(s4))
// hard
/*
给定字符串 str,检查其是否符合美元书写格式
1.以 $ 开始
2.整数部分,从个位起,满 3 个数字用 , 分隔
3 如果为小数,则小数部分长度为 2
4 正确的格式如:$1,023,032.03 或者 $2.03,错误的格式如:$3,432,12.12 或者 $34,344.3**
*/
let s5: string[] = ['$1,023,032.03', '$2.03'];
function f5(s: string): boolean {
const regx = /^\$\d{1,3}(,\d{3}){0,}(\.\d{2}){0,1}$/;
return regx.test(s)
}
if (s5.every(val => f5(val))) {
console.log('5', true)
}
/* JS实现千位分隔符 */
let s6: number = 21312312;
function f6(s: number) {
const sStr: string = s.toString();
// const regx = /\d{1,3}(?=(\d{3})+$)/g;
const regx = /\d{1,3}(?=(\d{3}){1,}$)/g;
// return sStr.replace(regx,'$&,'); // $&表示与regx相匹配的字符串
return sStr.replace(regx, (x) => {
// console.log('f6', x)
return `${x},`
})
}
console.log('6', f6(s6));
/*
获取 url 中的参数
1 指定参数名称,返回该参数的值 或者 空字符串
2 不指定参数名称,返回全部的参数对象 或者 {}
3 如果存在多个同名参数,则返回数组
*/
let s7 = '/zentao/js/chartjs/chart.line.min.js?v=pro6.7.3&_=1553315640088';
function getUrlParams(url: string) {
const arr = {};
const regx = /\?{0,1}(\w{1,})=(\w{1,})&?/g;
url.replace(regx, (match, matchKey, matchValue): string => {
console.log('getUrlParams', match, matchKey, matchValue);
if (!arr[matchKey]) {
arr[matchKey] = matchValue;
} else {
const temp = arr[matchKey];
arr[matchKey] = [].concat(temp, matchValue);
}
return '';
})
return arr;
}
console.log(getUrlParams(s7));
/* 验证邮箱 */
let s8 = '[email protected]';
function isEmail(email: string) {
const regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
return regx.test(email);
}
console.log(isEmail(s8));
/* 密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符 */
function checkPassword() {
const regx = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
}
/* 过滤HTML标签 */
let str: string = "<p>dasdsa</p>nice <br> test</br>"
function filterHtml(str: string) {
var regx = /<[^<>]+>/g;
return str.replace(regx, '');
}
console.log('str', filterHtml(str))
收集到的正则匹配(2):https://www.kancloud.cn/zhangqh/front/649501
function formatDate(t,str){
const obj = {
yyyy:t.getFullYear(),
yy:(""+ t.getFullYear()).slice(-2),
M:t.getMonth()+1,
MM:("0"+ (t.getMonth()+1)).slice(-2),
d:t.getDate(),
dd:("0" + t.getDate()).slice(-2),
H:t.getHours(),
HH:("0" + t.getHours()).slice(-2),
h:t.getHours() % 12,
hh:("0"+t.getHours() % 12).slice(-2),
m:t.getMinutes(),
mm:("0" + t.getMinutes()).slice(-2),
s:t.getSeconds(),
ss:("0" + t.getSeconds()).slice(-2),
w:['日', '一', '二', '三', '四', '五', '六'][t.getDay()]
};
return str.replace(/([a-z]+)/ig,function($1){return obj[$1]});
}
function rgb2hex(sRGB) {
return sRGB.replace(/^rgb\((\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\)$/g, function(a, r, g, b){
return '#' + hex(r) + hex(g) + hex(b);
});
}
function hex(n){
return n < 16 ? '0' + (+n).toString(16) : (+n).toString(16);
}
function cssStyle2DomStyle(sName) {
return sName.replace(/(?!^)\-(\w)(\w+)/g, function(a, b, c){
return b.toUpperCase() + c.toLowerCase();
}).replace(/^\-/, '');
}
function count(str) {
var obj = {};
str.replace(/\S/g,function(s){
!obj[s]?obj[s]=1:obj[s]++;
})
return obj;
}
Dan Abramov(React 核心开发,Redux 作者) 谈他不懂的技术这篇文章结尾说的很好,这是一个贩卖焦虑的时代,最应该静下心来专研某一方面,这是我的理解
react 前端项目技术选型、开发工具、周边生态
const detectDeviceType = () =>
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
? 'Mobile'
: 'Desktop';
// 事例
detectDeviceType(); // "Mobile" or "Desktop"
export const cached = (fn: any) => {
// Create an object to store the results returned after each function execution.
const cache = Object.create(null);
// Returns the wrapped function
return function cachedFn(str: any) {
// If the cache is not hit, the function will be executed
if (!cache[str]) {
const result: any = fn(str);
// Store the result of the function execution in the cache
cache[str] = result;
}
return cache[str];
};
};
涉及到了 DNS,TCP 连接,HTTP 请求等;
具体的细节涉及到 HTTP 状态码,重绘以及回流的优化等等
之后测试性能与版本的关系可以在 这个网站进行测试
https://overreacted.io/how-are-function-components-different-from-classes/
关于react中class与function写法的区别以及 hooks一些知识
可以看这里 https://codesandbox.io/s/nw5xlozk6l
具体实现分为两步:
问题1:
if([2] > 1){console.log('猜猜 我能不能打印出来!')}
new function(){console.log(this)}
问题2:
const foo = {
bar: function () {
console.log(this)
}
}
(foo.bar)(); // {bar: ƒ}
(false || foo.bar)(); // Window
(foo.bar = foo.bar)(); // Window
问题3:
async function async1() {
console.log('async1 start')
await async2();
console.log('async1 end')
}
async function async2() {
console.log('async2')
}
console.log('script start')
setTimeout(function(){
console.log('setTimeout')
}, 0)
async1();
console.log('script end')
问题4: 主要是查看这里
null == undefined
false == undefined
false == null
false == ['']
ECMAScript的最新说明:https://tc39.github.io/ecma262/#sec-intro
关于js中Eventloop 具体看node官方中介绍: https://nodejs.org/es/docs/guides/event-loop-timers-and-nexttick/
以及浏览器中事件队列与之区别
Number.isNaN ('a') ; // false; | 单纯的判断是不是NaN
isNaN('a'); // true | 判断是不是数字
时间很快,2019的目标大部分都还没有完成,继续再接再厉!
了解 babel 具体操作
https://cnodejs.org/topic/5a9317d38d6e16e56bb808d1
尽量多的翻译 medium 的文章(暂定一个月两篇)
react 源码--关注一下 https://overreacted.io/
算法 :高级概念及实现的掌握(每周一题)
英语学习:https://github.com/byoungd/English-level-up-tips-for-Chinese/blob/master/part-1/2-vocabulary.md
webpack 5
关于 node 可以看狼叔的指导https://github.com/i5ting/How-to-learn-node-correctly
2020年05月06日15:28:18
记有一次面试,关于动画性能 CSS 动画之硬件加速
然后得出结论:
- transform 属性不会触发浏览器的 repaint(严格来说,应该是动画期间不会造成重绘,动画开始和结
束的时候发生了两次 repaint 操作。可以用 Chrome 的 rendering工具进行 repaint 和 reflow 的性能分
析),而 left 和 top 则会一直触发 repaint。
.parent{
display: flex;
justify-content: center;
align-items: center;
}
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
优先想到的是:伪类 + transform 实现 transform: scaleY(0.5);
其他看这里
触发条件,解释,看 mdn
简单罗列:
优先级(就近原则):!important > [ id > class > tag ]
!important > 内联 > ID > 类 > 标签 | 伪类 | 属性选择 > 伪对象 > 通配符 > 继承
具体也可以看看这位博主,写得很详细 https://github.com/LiangJunrong/document-library/blob/master/other-library/Interview/PersonalExperience/Other-%E5%9B%BE%E7%89%87.md
CSS 对象模型(CSSOM)
// 单行
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
// 多行
display: -webkit-box;
-webkit-box-orient:vertical;
-web-line-clamp:3;
overflow:hidden;
-webkit-box-orient: vertical 在使用 webpack 打包的时候这段代码会被删除掉,原因是 optimize-css-assets-webpack-plugin 这个插件的问题
看到的比较好介绍 react 中父子组件之间的生命周期的关系
react 源码分析 -- 关注一下 Dan 的博客
react 中引入 React Hot Loader 的功能分析:
之前可能也没太注意到,hot module replacement(下面简称 HMR)就是webpack的热更新了,为什么引入了这个热更新,是为了解决,更新之后组件会被销毁的问题,导致state初始化,当然redux的数据是不会变的
了解 babel 具体操作
https://cnodejs.org/topic/5a9317d38d6e16e56bb808d1
尽量多的翻译 medium 的文章(暂定一个月两篇)
react 源码--关注一下 https://overreacted.io/
算法 :高级概念及实现的掌握
英语学习:https://github.com/byoungd/English-level-up-tips-for-Chinese/blob/master/part-1/2-vocabulary.md
webpack
关于 node 可以看狼叔的指导https://github.com/i5ting/How-to-learn-node-correctly
2019年02月26日18:19:50
分页面、区域、浏览器、性能指标
我们需要在用户输入 URL 或者点击链接的时候就开始统计,因为这样才能衡量用户的等待时间。高端浏览器Navigation Timing接口;普通浏览器通过 cookie 记录时间戳的方式来统计,需要注意的是 Cookie 方式只能统计到站内跳转的数据。
目前,使用的 https://www.draw.io/ 也是非常方便的!
源于之前的一次面试,请看题
const a = {
count:1,
b:{
count: 2,
getCount: function(){
console.log(this.count);
}
}
}
const getb = a.b.getCount
console.log(getb())
console.log(a.b.getCount())
理解的话,首先要明确的是 this是在函数里面
来自这里 https://juejin.im/post/5c6bbf0f6fb9a049ba4224fd#heading-16
几个概念:堆栈,当前执行上下文(栈) (静态)作用域链
总结一句话,函数里面的this(非箭头函数): this 只有在运行时才会存在,且指向了当前执行上下文,在箭头函数里面:定义时绑定,this为参数,指向的是箭头函数上一层级作用域中那个this。
理解有误,请指正!
如何编写高质量的函数 -- 敲山震虎篇 ---详细介绍了函数中底层知识:
总结如下:
为什么是栈呢?先进后出(有递归能力) 可以很好的保存和恢复调用现场
深入理解浏览器的缓存机制
主要有:一些概念,强缓存,协商缓存
ETag、CacheControl、Expires
举个例子:浏览器第一次请求图片 a,服务器返回完整文件(以及附带信息,Etag 是对 a 文件的编码[一般 md5],只要 a 在服务器上没有被修改,这个值就不会变)
在返回头里面有如下信息
cache-control: public, max-age=300
etag: W/"FpzhXY6i2z0VU2fftJB289pZu1hS"
浏览器就会把 a(以及额外信息)保存到本地。浏览器在 300 秒以内再次需要获取 a 时,浏览器直接从缓存读取 a (200, from xx cache)。在 300 秒之后再次需要获取 a 时,浏览器发现该缓存过期, 就会重新请求服务器,此时发送就会附带上刚刚保存的 Etag( If-None-Match:W/"FpzhXY6i2z0VU2fftJB289pZu1hS")。服务器就会对比这两个 Etag ,不变的话状态码返回 304。不等则发送新文件和新的 ETag,浏览器获取新文件并更新该文件的 Etag。
与 ETag 类似功能的是 Last-Modified/If-Modified-Since。(服务器是集群,图片放在多台服务器上,每个服务器计算出来的 Etag 就不一样,请求的服务器是随机的,所以才会出现 Last-Modified/If-Modified-Since 解决这个问题)
HTTP 2:多路复用以及服务器推送
关于存储的 Cookie V.S. LocalStorage V.S. SessionStorage V.S. Session
前端开发的安全建议:https://hackernoon.com/10-security-tips-for-frontend-developers-oi4624ld
实现了一个Chip-8语言解释器,对理解计算机体系结构很有帮助:https://www.taniarascia.com/writing-an-emulator-in-javascript-chip8/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.