web3fans / interview-questions-master Goto Github PK
View Code? Open in Web Editor NEW前端面试知识点总结
前端面试知识点总结
极客时间上的一个课程,做成的思维导图(需翻墙)https://coggle.it/diagram/Xoq2oI9RpmDW90VQ/t/%E6%B5%8F%E8%A7%88%E5%99%A8
Proxy 是 ES6 中新增的功能,可以用来自定义对象中的操作。
Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。
//target表示用Proxy包装的目标对象(可以是任何类型的对象,包括原生数组,函数,甚至另一个代理)。handle表示一个对象,其属性是当执行一个操作时定义代理的行为的函数。
let p = new Proxy(target, handler);
//创建一个可撤销的Proxy对象
let q = Proxy.revocable();
//示例代码来自网络,有修改
//使用 Proxy 实现一个数据绑定和监听
let onWatch = (obj, setBind, getLogger) => {
let handler = {
get(target, property, receiver) {
getLogger(target, property)
return Reflect.get(target, property, receiver);
},
set(target, property, value, receiver) {
setBind(value);
return Reflect.set(target, property, value);
}
};
return new Proxy(obj, handler);
};
let obj = { a: 1 }
let value;
let p = onWatch(obj, (v) => {
value = v
}, (target, property) => {
console.log(`Get '${property}' = ${target[property]}`);
})
p.a = 2 // bind `value` to `2`
p.a // -> Get 'a' = 2
requestAnimationFrame告诉浏览器在下次渲染时调用指定的函数callback。
/**
* @description:
* @param {function} func
* @param {number} wait
* @param {boolean} immediate
* @return: {function}
*/
//underscore中源码
_.debounce = function (func, wait, immediate) {
var timeout, timestamp, result, context, args;
var later = function () {
var last = _.now() - timestamp;//_.now()获取当前时间
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
context = args = null;
}
}
}
return function () {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
应用场景为:滚动轴事件及提交按钮.网上也有很多实现,但最好的实现我觉得应该是underscore源码重的debounce函数。
/**
* @description:
* @param {function} func
* @param {number} wait
* @param {boolean} immediate
* @return: {function}
*/
//underscore中源码
_.debounce = function (func, wait, immediate) {
var timeout, timestamp, result, context, args;
var later = function () {
var last = _.now() - timestamp;//_.now()获取当前时间
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
context = args = null;
}
}
}
return function () {
context = this;
args = arguments;
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
instanceof 是判断原型链上是否存在当前对象的构造函数,具体实现为:
function instanceof (left, right){
var prototype = right.prototype;
left = left._proto_;
while(true){
if(left === null){
return false;
}
if(left === portotype){
return true;
}
left = left.prototype;
}
}
题目:
for ( var i=1; i<=5; i++) {
setTimeout( function timer() {
console.log( i );
}, i*1000 );
}
解决办法:
1.var改成let,创建块作用域
2.使用立即执行函数
for (var i = 1; i <= 5; i++) {
(function(j) {
setTimeout(function timer() {
console.log(j);
}, j * 1000);
})(i);
}
3.使用 setTimeout 的第三个参数
for ( var i=1; i<=5; i++) {
setTimeout( function timer(j) {
console.log( j );
}, i*1000, i);
}
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.