-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
字节面试题(day6) #25
Labels
Comments
/**
* 头条
*/
var length = 10;
function fn() {
console.log(this.length)
};
var obj = {
length: 5,
method: function (fn) {
fn();
arguments[0]();
fn.call(obj, 12);
}
};
obj.method(fn, 1); |
// ; 能不能实现 Primise.allSettled,解决这个文档,无论是正常请求还是异常请求都能被 resolve 返回;
Promise.prototype.allSettled = function (...args) {
let arr = []
const promise = new Promise(resolve => {
args.forEach((promise,index) => {
promise.then(res => {
arr[index] = ({
code: 0,
data: res
})
}).catch(error=> {
arr[index] = ({
code: -1,
error
})
}).finally(_ => {
if(arr.length === args.length) {
resolve(arr)
}
})
})
})
return promise
}
|
const template = "My name is ${name}, I`m from ${city}"
function print(template, obj) {
/**
* $0: match 匹配的字段
* $1: 捕获的结果
*/
return template.replace(/\${(\w+)}/g, ($0, $1) => {
console.log($0, $1)
return obj[$1]
})
}
const result = print(template, {
name: 'haha',
city: 'BeiJing'
})
console.log(result) // My name is hah, I`m from BeiJing |
// 头条
// sum(1, 2, 3).sumOf(); //6
// sum(2, 3)(2).sumOf(); //7
// sum(1)(2)(3)(4).sumOf(); //10
// sum(2)(4, 1)(2).sumOf(); //9
function sum() {
let args = Array.from(arguments)
const fn = function () {
args = args.concat(Array.from(arguments))
return fn
}
fn.sumOf = () => {
// 求和函数
return args.reduce((prev,cur) => {
return prev + cur
})
}
return fn
}
console.log(sum(1,2,3).sumOf())
console.log(sum(2,3)(2).sumOf())
console.log(sum(1)(2)(3)(4).sumOf())
console.log(sum(2)(4,1)(2).sumOf()) |
输出 10 2 5.
参考原理: 执行上下文栈 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
storage
事件监听storage
事件监听serviceworker 不能访问 DOM,他有自己独立的上下文环境 seft,它运行在其他线程中,因此,不能阻塞主进程的执行。
其他使用场景
The text was updated successfully, but these errors were encountered: