const arr = [1,4,9]
const newArr = arr.map((x)=>{
return x + this.a
},{a:1})
console.log(newArr) // [NaN, NaN, NaN]
window.name ="shopee"
function A(){
this.name = 123
}
A.prototype.getA = function (){
console.log(this)
return this.name + 1
}
let a = new A()
let funcA = a.getA;
funcA()
答案:shopee1
let obj1= {a:1,b:{c:2}}
let obj2 = {...obj1}
obj1.a = 3
console.log(obj1) // {a:3,b:{c:2}}
console.log(obj2) // {a:1,b:{c:2}}
obj2.a = 4
console.log(obj1) // {a:1,b:{c:2}}
console.log(obj2) // {a:4,b:{c:2}}
obj2.b.c = 5
console.log(obj1) // {a:1,b:{c:5}}
console.log(obj2) // {a:1,b:{c:5}
new Promise((resolve,reject)=> {
resolve('success')
})
.then(()=>{
throw Error('oh')
})
.catch(error =>{
return 'yes'
})
.catch(error => console.log(error.message))
.then(error => console.log(error.message))
答案:Promise
var p1 = new Promise(function(res,rej){
console.log('promise 1')
res()
})
setTimeout(function(){
console.log('setTime0')
})
var p2 = new Promise(function(res,rej){
setTimeout(()=>{
res(1)
},0)
})
setTimeout(function(){
console.log('setTime1')
})
for(let i = 0;i<3;i++){
p2.then(function(val){
console.log('promise then -' + i)
})
}
Promise.resolve.then(()=>{
console.log('promise then')
})
p1.then(function(){
console.log('promise 1 resolved')
})
console.log('finished')
答案:
promise 1
finished
promise 1 resolved
promise then
setTime0
promise then - 0
promise then - 1
promise then - 2
setTime1
function f1(){
console.time('time span')
}
function f2(){
console.timeEnd('time span')
}
setTimeout(f1,100)
setTimeout(f2,200)
function waitForMs(n){
var now = Date.now()
while(Date.now() - now < n){}
}
waitForMs(500)
答案:
time span:0.03ms