js基礎面試題 看似簡單也易出錯

1.
alert(a)
a()
var a=3
function a(){alert(10)}
alert(a)
a=6
a()



===》
function a(){alert(10)}  //變量提升a方法
10  第二步執行a 輸出10
3  a重新賦值 3
a is not a function  賦值a=6 不是一個function

2.
var lili={age:18};
    (function () {
      var xiaoming=lili;
      xiaoming.age=22;
      console.log(xiaoming.age);
      console.log(lili.age)
    } ) ()
    console.log(lili.age);
    console.log(xiaoming.age)
===》 22  22  22  

3.
function A() {
  this.name='a'
this.color=['green','yellow']
}
function B(){}
B.prototype=new A()
var b1=new B()
var b2=new B()
b1.name='change'
b2.color.push('red')
console.log(b1.name)
console.log(b2.name)
console.log(b1.color)
console.log(b2.color)
===》
change
a
["green", "yellow", "red"]
["green", "yellow", "red"]
4.
var length=10;
    function fn(){
      console.log(this.length)
    }
    var obj={
      length:5,
      methed:function(fn){
        fn() 
        arguments[0]()
      }
    }
    obj.methed(fn,1,2)
==>
10  //this爲window
3 arguments類數組長度

5.
const promise=new Promise((res,rej)=>{
    res('success')
    rej('error')
    res('success2')
  })
  promise.then((res)=>{
    console.log('then'+res)
  }).catch((err)=>{
    console.log('catch'+err)
  })
=》
  thensuccess

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章