前端 JS 笔试百题( 七 )

var a = 5; function test() {a = 0;alert(a);alert(this.a);var a;alert(a); }new test();

  1. 下列程序的输出结果是多少?为什么?
function fun () {return () => {return () => {return () => {console.log(this.name)}}}}var f = fun.call({name: 'foo'})var t1 = f.call({name: 'bar'})()()var t2 = f().call({name: 'baz'})()var t3 = f()().call({name: 'qux'})
  1. 执行以下代码,输出结果分别是多少?
let obj1 = {a: 1,foo: () => {console.log(this.a)}}// log1obj1.foo()const obj2 = obj1.foo// log2obj2()
  1. 下面程序的输出结果是什么?为什么?
const Person = (name="wang",age=10) => {this.name = name;this.age = age;return this.name +' is '+ this.age + 'years old'}let result = new Person('zhang',11)console.log(result)
  1. 请表述以下代码的执行结果和原因
var person = {age: 18,getAge: function() {return this.age;}};var getAge = person.getAgegetAge()
  1. 请按顺序写出打印结果,并说明原因 。
var name = 'global';var obj = {name: 'local',foo: function(){this.name = 'foo';}.bind(window)};var bar = new obj.foo();setTimeout(function() {console.log(window.name);}, 0);console.log(bar.name); var bar3 = bar2 = bar;bar2.name = 'foo2';console.log(bar3.name);
  1. 下面程序的执行结果是:
var obj = {name:"zhangsan",sayName:function(){console.info(this.name);}}var wfunc = obj.sayName;obj.sayName();wfunc();var name = "lisi";obj.sayName();wfunc();
  1. 下面程序的输出结果是:
var name='test' var a = {name: 'ass',getName: function() {return this.name;} } var b = a.getName; b();事件循环
  1. 下列程序的输出结果分别是多少?为什么?
const promiseA = Promise.resolve('a')promiseA. then((res) => {console.log(res)}).then((res) => {console.log(res)})const promiseB = Promise.resolve('b')promiseB. then((res) => {console.log(res)})promiseB. then((res) => {console.log(res)})
  1. 下面程序的输出结果依次是多少?
setTimeout(() => {console.log(1)}, 0)const P = new Promise((resolve, reject) => {console.log(2)setTimeout(() => {resolve()console.log(3)}, 0)})P.then(() => {console.log(4)})console.log(5)
  1. 下面程序的输出结果是
setTimeout(function(){console.log(1);}, 0)new Promise(function(resolve){console.log(2);resolve();console.log(3);}).then(function(){console.log(4);})console.log(5);
  1. 下面程序的输出结果是?
(async () => {console.log(1);setTimeout(() => {console.log(2);}, 0);await new Promise((resolve, reject) => {console.log(3);}).then(() => {console.log(4);});console.log(5);})();
  1. 下面程序的输出结果是:
new Promise((resolve) => {console.log('1')resolve()console.log('2')}).then(() => {console.log('3')})setTimeout(() => {console.log('4')})console.log('5')
  1. 下面程序的输出结果是:
var p1 = new Promise(function(resolve, reject){resolve("2")})setTimeout(function(){console.log("1")},10)p1.then(function(value){console.log(value)})setTimeout(function(){console.log("3")},0)
  1. 下面程序的输出结果是:
setTimeout(function() {console.log('setTimeout');}, 0);Promise.resolve().then(function() {console.log('promise1');}).then(function() {console.log('promise2');});
  1. 请表述以下代码的执行结果和原因
setTimeout(function() {console.log(1)},0)new Promise(function executor(resolve){console.log(2)for (var i = 0; i<10000; i++) {i - 9999 && resolve()}console.log(3)}).then(function() {console.log(4)})console.log(5)
  1. 在网页中有两个div块,html代码如下
<div class="outer"> <div class="inner"></div></div>对应的js代码如下:
var outer = document.querySelector('.outer');var inner = document.querySelector('.inner');function onClick() {console.log('click');setTimeout(function() {console.log('timeout');}, 0);Promise.resolve().then(function() {console.log('promise');});outer.setAttribute('data-random', Math.random());}inner.addEventListener('click', onClick);outer.addEventListener('click', onClick);


推荐阅读