一、
//定义一个Foo函数
function Foo() {
getName = function () { alert(1) };
return this;
}
Foo.getName = function () { alert(2) };
Foo.prototype.getName = function () { alert(3) };
//函数表达式
var getName = function () { alert(4) }
//函数声明
function getName() { alert(5) }
//请写出输出结果
Foo.getName() //打印 2
getName() //打印 4
Foo().getName() //打印 1
getName() //打印 1
new Foo.getName() //打印 2
new Foo().getName() //打印 3
new new Foo().getName() //打印 3
二、
async function async1(){
console.log(‘async1 start’)
await async2()
console.log(‘async1 end’)
}
async function async2() {
console.log(‘async2’)
}
console.log(‘script start’)
setTimeout(() => {
console.log(‘setTimeout’)
});
async1()
new Promise((resolve=>{
console.log(‘promise1’)
resolve()
})).then(function () {
console.log(‘promise2’)
})
console.log(‘script end’)
运行结果
- script start
- async1 start
- async2
- promise1
- script end
- async1 end
- promise2
- setTimeout