js面试题

219 阅读2分钟
let a = {}, b = '0', c = 0;
a[b] = '123'
a[c] = '234'
console.log(a[b])
let a = {}, b = Symbol('1'), c = Symbol('1');
a[b] = 123
a[c] = 234
console.log(a[b])
let a = {}, b = {age: 18}, c = {age: 19};
a[b] = 123
a[c] = 234
console.log(a[b])
let test = (function (i) {
  return function () {
    alert(i*=2)
  }
})(2)
test(5)
var a = 0, b = 0
function A(a) {
  A = function(b) {
    console.log(a + b++)
  }
  console.log(a++)
}
A(1)
A(2)
function Foo() {
  getName = function() {
    console.log(1)
  }
  return this
}
Foo.getName = function () {
  console.log(2)
}
Foo.prototype.getName = function () {
  console.log(3)
}
var getName = function() {
  console.log(4)
}

function getName() {
  console.log(5)
}

Foo.getName()
getName()
Foo().getName()
getName()
new Foo.getName()
new Foo().getName()
new new Foo().getName()
async function async1() {
  console.log('async1 start')
  await async2()
  console.log('async1 end')
}
async function async2() { 
  console.log('async2')
}
console.log('start')
setTimeout(function(){
  console.log('setTimeout')
}, 0)
async1()
new Promise((resolve)=>{
  console.log('Promise1')
  resolve()
}).then(()=>{
  console.log('Promise2')
})
console.log('end')

function A() {
    alert(1)
}
function Func() {
    A = function() {
            alert(2)
    }
    return this
}
Func.A = A
Func.prototype = {
    A:()=>{
       alert(3)
    }
}
A() // 1
Func.A() // 1
Func().A() // 2
new Func.A() // 1
new Func().A() //3
new new Func().A()//报错 箭头函数不能被new,箭头函数没有原型链
当啊等于什么的时候,下面条件成立
if(a == 1 && a == 2 && a == 3) {
    console.log('条件成立')
}

第一种方法:
let a = {
    i: 0,
    toString() {
        return ++this.i
    }
}

第二种方法:
let a = {
    i: 0,
    valueOf() {
       return ++this.i
    }
}

第三种方法:
let i = 0
Object.defineProperty(window, 'a', {
    get() {
       return ++i
    }
})

第四种方法:
let a = [1, 2, 3]
a.toString = a.shift
let x = 2
let y = {
    x: 3,
    z:(function(x){
            this.x*=x
            x+=2
            return function(n) {
                    this.x*=n
                    x+=3
                    console.log(x) // 7 10
            }

    })(x)
}
let m = y.z
m(4)
y.z(5)
console.log(x,y.x) // 2 15
function fn(a, c) {
    console.log(a) // function a() {} 
    var a = 123
    console.log(a) // 123
    console.log(c)// function c() {}
    function a() {} 
    if(false) {
            var d = 678
    }
    console.log(d) // undefined
    console.log(b) // undefined
    var b = function () {}
    console.log(b) // function () {}
    function c() {}
    console.log(c) // function c() {}

}
fn(1, 2)

// 预编译
//形成作用域的创建阶段 预编译阶段
// 预编译的时候做了那些事情?
// js的变量对象 AO对象 供js引擎自己去访问的
// 1、创建了A0对象
// 2、找形参和变量声明 作为AO对象的属性名 值是undefined
// 3、实参和形参相统一
// 4、找函数声明 会覆盖变量声明

/* AO: {
        a: undefined -> function a()
        b: undefined 
        c: undefined -> function c()
        d: undefined
} */

// js的执行
var name = 222
var a = {
        name: 111,
        say: function () {
                console.log(this.name)
        }
}
var fun = a.say
fun() // 222 fun.call(window)
a.say() // 111 a.say.call(a) 
var b = {
        name: 333,
        say: function (fun) {
                fun()
        }
}
b.say(a.say) // 222 fun.call(window)
b.say = a.say 
/* var b = {
    name: 333,
    say: function () {
            console.log(this.name)
    }
} */
b.say() // 333