前端 JS 笔试百题( 九 )

for (var i = 0; i < 3; i++) {setTimeout(_ => {console.log(i)})}for (let i = 0; i < 3; i++) {setTimeout(_ => {console.log(i)})}

  1. 下面程序的输出结果是:
console.log(a);var a = 'a';console.log(b);let b = 'b';
  1. 下面程序的输出结果是:
var foo = "Hello";(function(){var bar = " World";alert(foo + bar);})();alert(foo + bar);
  1. 下面程序的输出结果是:
var a = 10;(function () {console.log(a)a = 5console.log(window.a)var a = 20;console.log(a)})()
  1. 下面代码的输出结果是:
const a = 10function runFunction() {const a = 20console.log('inside', a)}runFunction()console.log('outside', a)
  1. 请描述打印结果并说明原因
"use strict"var name = 'Jay'var person = {name: 'Wang',pro: {name: 'Michael',getName: function () {return this.name}}}console.log(person.pro.getName)var people = person.pro.getNameconsole.log(people())
  1. 下面程序的结果是:
<ul><li>1</li><li>2</li><li>3</li><li>4</li></ul><script>var elements = document.getElementsByTagName("li");for (var i=0;i<elements.length;i++){elements[i].onclick =function( ){alert(i);};}
  1. 下面程序的输出结果是
compute(10,100);var compute = function(A,B) {console.info(A * B) ;};function compute(A,B){console.info(A + B);}function compute(A,B){console.info((A + B)*2);}compute(2,10);
  1. 下面程序的执行结果是:
meili()function meili() {console.log("meili")}mogu()var mogu = function() {console.log("mogu")}
  1. 下面两个代码片段输出结果有什么区别?为什么?
// 片段1check('first');function check(ars){console.log(ars);}// 片段2check('second');var check= function(ars){console.log(ars);}ES6对象
  1. 下面代码的输出结果是?
const student = {name: 'ZhangSan'}Object.defineProperty(student, 'age', {value: 22})console.log(student)console.log(Object.keys(student))generator
  1. 下列程序的输出结果是多少?为什么?
function * cb(x, y) {for(let i = Math.ceil(x); i <= y; i++) {yield i;}}var a = cb(6, 9);console.log(a.next());console.log(a.next());扩展运算符
  1. 下面程序的输出结果是:
function fn(...args) {console.log(typeof args);}fn(21);promisePromise.reject(0).catch(e => e).catch(e => console.log(e))class
  1. 请写出下面ES6代码编译后所生成的ES5代码
class Person {constructor (name) {this.name = name;}greet () {console.log(`Hi, my name is ${this.name}`);}greetDelay (time) {setTimeout(() => {console.log(`Hi, my name is ${this.name}`);}, time);}}标签模板
  1. 下面程序的输出结果是多少?
function getPersonInfo (one, two, three) {console.log(one)console.log(two)console.log(three)}const person = 'Lydia'const age = 21getPersonInfo `${person} is ${age} years old`module
  1. 请写出index里面的输出结果
// module.jsexport default () => "Hello world"export const name = "nowcoder"// index.jsimport * as data from "./module"console.log(data)
  1. 有a.js和b.js两个文件,请写出b文件中代码的输出
// a.jslet a = 1let b = {}setTimeout(() => {a = 2b.b = 2}, 100)module.exports = { a, b }// b.jsconst a = require('./a')console.log(a.a)console.log(a.b)setTimeout(() => {console.log(a.a)console.log(a.b)}, 500)其他
  1. 输出结果是:
<div id="box1"><div id="box2">content</div></div><script>const $ = document.querySelector.bind(document);const box1 = $('#box1');const box2 = $('#box2');box1.addEventListener('click', () =>{console.log('box1 true');}, true);box1.addEventListener('click', () =>{console.log('box1 false');}, false);box2.addEventListener('click', () =>{console.log('box2 true');}, true);box2.addEventListener('click', () =>{console.log('box2 false');}, false);</script>


推荐阅读