包含类型判断、作用域、this指向、原型、事件循环等知识点
请问下面的代码输出结果是什么? 第1题 let a = 1 function b (a ) { a = 2 console .log (a) } b (a)console .log (a)
答案:2 1
第2题 function a (b = c, c = 1 ) { console .log (b, c) } a ()
报错:Cannot access ‘c’ before initialization
第3题 let a = b = 10 ;(function ( ){ let a = b = 20 })() console .log (a)console .log (b)
答案:10、20
第4题 var a = {n :1 }var b = aa.x = a = {n :2 } console .log (a.x )console .log (b.x )
答案:undefined、{n: 2}
第5题 var arr = [0 , 1 , 2 ]arr[10 ] = 10 console .log (arr.filter (function (x ) { return x === undefined }))
答案:[ ]
第6题 var name = 'World' ;(function ( ) { if (typeof name === 'undefined' ) { var name = "Jack" console .info ('Goodbye ' + name) } else { console .info ('Hello ' + name) } })()
答案:Goodbye Jack
第7题 console .log (1 + NaN )console .log ("1" + 3 )console .log (1 + undefined )console .log (1 + null )console .log (1 + {})console .log (1 + [])console .log ([] + {})
答案:NaN、13、NaN、1、1[object Object]、1、[object Object]
第8题 var a={}, b={key :'b' }, c={key :'c' } a[b]=123 a[c]=456 console .log (a[b])
答案:456
第9题 var out = 25 var inner = { out : 20 , func : function ( ) { var out = 30 return this .out } }; console .log ((inner.func , inner.func )())console .log (inner.func ())console .log ((inner.func )())console .log ((inner.func = inner.func )())
答案:25、20、20、25
第10题 let {a,b,c} = { c :3 , b :2 , a :1 }console .log (a, b, c)
答案:1、2、3
第11题 console .log (Object .assign ([1 , 2 , 3 ], [4 , 5 ]))
[4, 5, 3]
第12题 var x=1 switch (x++){ case 0 : ++x case 1 : ++x case 2 : ++x } console .log (x)
答案:4
第13题 console .log (typeof undefined == typeof NULL )console .log (typeof function ( ) {} == typeof class {})
答案:true、true
第14题 var count = 0 console .log (typeof count === "number" )console .log (!!typeof count === "number" )
答案:true、false
第15题 "use strict" a = 1 var a = 2 console .log (window .a )console .log (a)
答案:2、2
第16题 var i = 1 function b ( ) { console .log (i) } function a ( ) { var i = 2 b () } a ()
答案:1
第17题 var obj = { name : 'abc' , fn : () => { console .log (this .name ) } }; obj.name = 'bcd' obj.fn ()
答案:undefined
第18题 const obj = { a : { a : 1 } }; const obj1 = { a : { b : 1 } }; console .log (Object .assign (obj, obj1))
答案:{a: {b: 1}}
第19题 console .log (a)var a = 1 var getNum = function ( ) { a = 2 } function getNum ( ) { a = 3 } console .log (a)getNum ()console .log (a)
答案:undefined、1、2
第20题 var scope = 'global scope' function a ( ){ function b ( ){ console .log (scope) } return b var scope = 'local scope' } a ()()
答案:undefined
第21题 function fn (){ console .log (this ) } var arr = [fn]arr[0 ]()
答案:打印出arr数组本身
第22题 var a = 1 function a ( ){}console .log (a)var bfunction b ( ){}console .log (b)function b ( ){}var bconsole .log (b)
答案:1、b函数本身、b函数本身
第23题 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 ()
答案:2、4、1、1、2、3、3
第24题 const person = { address : { country :"china" , city :"hangzhou" }, say : function ( ) { console .log (`it's ${this .name} , from ${this .address.country} ` ) }, setCountry :function (country ) { this .address .country =country } } const p1 = Object .create (person)const p2 = Object .create (person)p1.name = "Matthew" p1.setCountry ("American" ) p2.name = "Bob" p2.setCountry ("England" ) p1.say () p2.say ()
答案:it’s Matthew, from England
it’s Bob, from England
第25题 setTimeout (function ( ) { console .log (1 ) }, 0 ) new Promise (function (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 )
答案:2、3、5、4、1
第26题 注:process是Node.js提供的一个对象,它代表当前Node.js进程
console .log ('1' );setTimeout (function ( ) { console .log ('2' ); process.nextTick (function ( ) { console .log ('3' ); }); new Promise (function (resolve ) { console .log ('4' ); resolve (); }).then (function ( ) { console .log ('5' ); }); }); process.nextTick (function ( ) { console .log ('6' ); }); new Promise (function (resolve ) { console .log ('7' ); resolve (); }).then (function ( ) { console .log ('8' ); }); setTimeout (function ( ) { console .log ('9' ); process.nextTick (function ( ) { console .log ('10' ); }) new Promise (function (resolve ) { console .log ('11' ); resolve (); }).then (function ( ) { console .log ('12' ) }); })
答案:1、7、6、8、2、4、3、5、9、11、10、12
1-26 来源:https://mp.weixin.qq.com/s/fks9SuTSY0ivUfPn6G-TOw