n1n9j1eyuan / interview-questions Goto Github PK
View Code? Open in Web Editor NEW每日一道面试题
每日一道面试题
📅 2021 / 03 / 28
在事件被触发 n 秒后再执行回调,如果在这 n 秒内又被触发,则重新计时。
const debounce = (fn, delay = 1000) => {
let timer = null;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
规定在一个单位时间内,只触发一次函数。如果这个单位事件内触发多次函数,只有一次生效。
const throttle = (fn, delay = 1000) => {
let timer = null;
return (...args) => {
if (timer) return
timer = setTimeout(() => {
timer = null;
fn.apply(this, args);
}, delay)
}
}
📅 2021 / 03 / 27
var str = 'ning-san-fen'
function fn(str) {
var arr = []
if (str.indexOf('-') > -1) arr = str.split('-');
for(var i = 1; i < arr.length; i++) {
arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1);
}
return arr.join('');
}
console.log(fn(str)) // ningSanFen
📅 2021/03/26
var a = {
value: 1,
toString: function () {
return a.value++;
}
};
console.log(a == 1 && a == 2 && a == 3); // true
var a = {
value: 1,
valueOf: function () {
return a.value++;
}
}
console.log(a == 1 && a == 2 && a == 3); // true
原理:Object 类型与 Number 类型比较时,会将 Object 类型转换为 Number 类型。转换时会尝试调用 Object.valueOf 和 Object.toString 来获取对应的数字基本类型。重写 toString 方法或者 valueOf 方法都可以实现该效果。
var a = [1, 2, 3];
a.join = a.shift;
console.log(a == 1 && a == 2 && a == 3); // true
console.log(a) // []
原理:数组的 toString 会隐含调用 Array.join 方法。数组的 shift 方法会将数组中第一个元素删除,并返回该元素的值。所以我们看到 a == 1 时,会调用 join 方法,因为 join 方法被重写为 shift 方法,所以转换为 Number 后为 1。
var val = 0;
Object.defineProperty(window, 'a', {
get: function() {
return ++val;
}
})
console.log(a === 1 && a === 2 && a === 3); // true
function A () {
var value = 0;
this.valueOf = function () {
return ++value;
}
}
var a = new A();
console.log(a == 1 && a == 2 && a == 3); // true
// ES6 版本
class A {
constroctar() {
this.value = 0;
this.valueOf()
}
valueOf() {
return this.value++;
}
}
var a = new A();
console.log(a == 1 && a == 2 && a == 3); // true
// 实现 add(1)
function add(a) {
return a;
}
// 实现 add(1)(2)
function add(a) {
return function (b) {
a = a + b;
return a;
}
}
// 实现 add(1)(2)(3)
function add(a) {
return function (b) {
a = a + b;
return function () {
a = a + c;
return a;
}
}
}
// 实现 add(1)(2)(3)...(10)
function add (a) {
var fn = function (b) {
a = a + b
return fn;
}
fn.toString = function () { return a };
return fn;
}
// // 实现 add(1, 2)(2)(3)...(10) 思路:使用 reduce 计算拼接的参数
function add() {
var args = [].slice.call(arguments);
var fn = function () {
var args_fn = [].slice.call(arguments);
return fn.apply(null, args.concat(args_fn));
}
fn.toString = function () {
return args.reduce((a, b) => a + b);
}
return fn;
}
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.