-
if (!("a" in window)) {
var a = 1;
}
console.log(a);
复制代码
-
var a = 1,
b = function a(x) {
x && a(--x)
};
console.log(a);
复制代码
-
function b(x, y, a) {
a = 8;
arguments[2] = 10;
console.log(a);
}
b(1, 2, 3);
复制代码
-
(function f(f) {
console.log((typeof f()));
})(function () { return 1; })
复制代码
-
var car = {
price: 15,
run: function () {
return this.price;
}
}
console.log(typeof (f = car.run)());
复制代码
-
var a = 1;
if (function () {}) {
a += typeof f;
}
console.log(a);
复制代码
-
var obj = {
value: 3,
fun: function () {
return this.value;
}
};
var fun = function () {
this.value = this.fun.apply(window) + "1";
}
fun.prototype = obj;
console.log(new fun().value);
复制代码
-
class A {
constructor() {
this.a = 1;
}
aFun() {
}
}
class B extends A {
constructor() {
super();
this.b = 2;
}
bFun() {
}
}
let obj = new B();
console.log(window.B);
复制代码
-
let arr = [];
setTimeout(function () {
arr.push(1);
});
Promise.resolve().then(() => {
arr.push(2);
});
new Promise(() => {
arr.push(7);
});
async function a() {
arr.push(3);
await b();
arr.push(4);
}
async function b() {
arr.push(5);
}
a();
arr.push(6);
setTimeout(() => {
console.log(arr.join(''));
});
复制代码
- 用promise写一个retry方法,如一个接口最多请求10次,当有请求成功时,就不在请求
function retry(fun, num) {
if (num <= 0) return;
fun().then(res => {
console.log(res, num);
}).catch(() => {
retry(fun, num - 1)
})
}
retry(function () {
return new Promise(function (resolve, reject) {
let flag = Math.random() * 10;
if (flag > 5) {
resolve('okok');
} else {
reject();
}
})
}, 10)
复制代码