记一次面试

来源:互联网 发布:李安 知乎 编辑:程序博客网 时间:2024/06/05 20:09

1.作用域问题

function b() {
console.log(myVar)
console.log(myVar1)
}
function a() {
var myVar = 2
var myVar1 = 3
b()
}
var myVar = 1
a()
console.log(myVar1)

2.原型及继承

function Tree() {}

Tree.prototype.MoreLeaf = function() {

this.leaf++

console.log('i have' + this.leaf + 'Leafs')

}

Tree.prototype.leaf = 0

var tree1 = new Tree()

tree1.MoreLeaf()

tree1.MoreLeaf()

var tree2 = new Tree()

tree2.MoreLeaf()

输出:


实现AppleTree继承Tree,实现确认AppleTree是继承Tree


3.闭包问题

for (var i=1; i<=3; i++) {

setTimeout(function() {

console.log(i)

}, 1000)

}


4.js事件执行顺序

下面事件在页面执行以后,10秒之内连续点击3次,输出结果如何

function a() {

var ms = 1000 + new Date().getTime()

while(new Date() < ms) {}

console.log('event waiting')

}


function b() {

console.log('event click')

}


document.addEventListener('click', b)

a()

console.log('event exuted')


5. 实现函数,用来判断两个参数相等