填空题
变量及参数相关
答案:① undefined ② Uncaught ReferenceError:Cannot access 'age' before initialization
function sayHi(){
console.log(name);
console.log(age);
var name="Lydia";
let age=21;
}
sayHi()
答案:① 2 ② 2 ③ Uncaught TypeError:fun is not a function
function fun(){console.log(1)};
fun();
function fun(){console.log(2)};
fun();
var fun=100;
fun();
答案:① function a(){} ② 2
function fn(a){
console.log(a);
var a=2;
function a(){};
console.log(a);
}
fn(1);
答案:① function a(){} ② 50
{
function a(){};
a=50;
}
console.log(a);
{
b=50;
function b(){};
}
console.log(b);
答案:① 10 ② Uncaught ReferenceError:b is not defined ③ 不执行
var f=true;
if(f===true){
var a=10;
}
function fn(){
var b=20;
c=30;
}
fn();
console.log(a);
console.log(b);
console.log(c);
答案:10
if('a' in window){
var a=10;
}
alert(a);
答案:undefined 11
var y=10;
if(!(x in window)){
var x=10;
}else{
++y;
}
alert(x);
alert(y);
答案:① NaN ② undefinedhello
var a=10;
a.pro=10;
console.log(a.pro+a);
var a='hello';
a.pro='world';
console.log(a.pro+a);
答案:3 1
var x=1;
function foo(x,y=function(){x=2}){
var x=3;
y();
console.log(x);
}
foo();
console.log(x);
答案:① 2 1 ② 5 ③ 1 1
var x=0,
y=1;
function fn(){
x+=2;
fn=function(y){
console.log(y+(--x));
};
console.log(x,y);
};
fn(3);
fn(4);
console.log(x,y);
答案:'undefined' 'number'
(()=>{
let x = (y = 10);
})();
console.log(typeof x);
console.log(typeof y);
当a等于什么值的时候下面代码不报错?if(a==1 && a==2 && a==3){console.log(1);}
方法一:toString或valueOf
var a={
i:0,
toString(){
return ++this.i;
}
}
var a=\[1,2,3\];
a.toString=a.shift;
方法二:数据劫持
var i=0;
Object.defineProperty(window,'a',{
get(){
return ++i;
}
});
答案:Jim
var name='Jim';
function person(){
console.log(name);
}
function test(){
var name='Tom';
person();
}
test()
答案:undefined
var temp='hello world';
function example(){
console.log(temp);
var temp='Aralic';
}
example();
答案:false true
const name = 'Lydia';
age = 21;
console.log(delete name);
console.log(delete age);
答案:10 10
let num = 10;
const increaseNumber = () => num++;
const increasePassedNumber = number => number++;
const num1 = increaseNumber();
const num2 = increasePassedNumber(num1);
console.log(num1);
console.log(num2);
答案:20 20 20 40
const value = {number:10};
const multiply = (x={...value})=>{
console.log(x.number \*= 2);
};
multiply();
multiply();
multiply(value);
multiply(value);
对象相关
答案:222
let a={},
b='0',
c=0;
a\[b\]='111';
a\[c\]='222';
console.log(a\[b\]);
答案:111
let a={},
b=Symbol('0'),
c=Symbol(0);
a\[b\]='111';
a\[c\]='222';
console.log(a\[b\]);
答案:222
let a={},
b={
n:'1'
},
c={
m:'2'
};
a\[b\]='111';
a\[c\]='222';
console.log(a\[b\]);
答案:Uncaught TypeError: newColor is not defined
class Chameleon{
static colorChange(newColor){
this.newColor=newColor;
}
constructor({newColor="green"}={}){
this.newColor=newColor;
}
}
const freddie=new Chameleon({newColor:"purple"});
freddie.colorChange("orange")
答案:{name:'Tom',info:{height:178,weight:70}} {name:'Jim',info:{height:178,weight:70}}
var person={
name:'Jim',
info:{
height:180,
weight:70
}
}
var student=Object.assign({},person);
student.name='Tom';
student.info.height=178;
console.log(student);
console.log(person);
答案:{name:'Lydia'},'1997'
function getInfo(member,year){
member.name='Lydia';
year='1998';
}
const person={name:'Sarah'};
const birthYear='1997';
getInfo(person,birthYear);
console.log(person,birthYear);
答案:'Oh no an error:Hello world!'
function greeting(){
throw 'Hello world!';
}
function sayHi(){
try{
const data=greeting();
console.log('It worked!',data);
}catch(e){
console.log('Oh no an error!',e);
}
}
sayHi()
答案:'Maserati'
function Car(){
this.make='Lamborghini';
return {make:'Maserati'};
}
const myCar=new Car();
console.log(myCar.make);
答案:{name:'Lydia',age:21},['name']
const person={name:'Lydia'};
Object.defineProperty(person,'age',{value:21});
console.log(person);
console.log(Object.keys(person));
答案:"{"level":19,"health":90}"
const settings={
username:'lydiahallie',
level:19,
health:90
};
const data=JSON.stringify(settings,\['level','health'\]);
console.log(data);
EventLoop问题
答案:① 2 ② 6 ③ 5 ④ 3 ⑤ 4 ⑥ 1
setTimeout(function(){console.log(1)},0)
new Promise(function(resolve,reject){
console.log(2);
resolve();
})
.then(function(){console.log(3)})
.then(function(){console.log(4)})
process.nextTick(function(){console.log(5)});
console.log(6)
答案:A B
var p3=new Promise(function(resolve,reject){
resolve('B');
});
var p1=new Promise(function(resolve,reject){
resolve(p3);
});
p2=new Promise(function(resolve,reject){
resolve('A');
});
p1.then(function(v){
console.log(v);
});
p2.then(function(v){
console.log(v);
});
答案:2 1
setTimeout(()=>{
console.log(1);
});
new Promise((resolve,reject)=>{
resolve();
}).then(()=>{
console.log(2);
});
答案:10 20
function \*generator(i){
yield i;
yield i\*2;
}
const gen=generator(10);
console.log(gen.next().value);
console.log(gen.next().value);
答案: "two"
const firstPromise=new Promise((res,rej)=>{
setTimeout(res,500,'one')
});
const secondPromise=new Promise((res,rej)=>{
setTimeout(res,100,'two')
});
Promise.race(\[firstPromise,secondPromise\]).then(res=>console.log(res))
(头条)答案:① script start ② async1 start1 ③ async2 ④ promise1 ⑤ script end ⑥ async1 end ⑦ promise2 ⑧ setTimeout
async function async1(){
console.log('async1 start1');
await async2()
console.log('async1 end');
}
async function async2(){
console.log('async2')
}
console.log('script start');
setTimeout(function(){
console.log('setTimeout')
},0)
async1();
new Promise(function(resolve){
console.log('promise1')
resolve()
}).then(function(){
console.log('promise2')
})
console.log('script end');
答案:① script start ② script end ③ promise1 ④ promise2 ⑤ setTimeout
console.log('script start');
setTimeout(function(){
console.log('setTimeout');
},0);
Promise.resolve().then(function(){
console.log('promise1');
}).then(function(){
console.log('promise2')
});
console.log('script end')
答案:① b ② e ③ c ④ d ⑤ a ⑥ f
setTimeout(()=>console.log('a'),0);
var p=new Promise(resolve=>{
console.log('b');
setTimeout(()=>console.log('f'),0);
resolve();
});
p.then(()=>console.log('c'));
p.then(()=>console.log('d'));
console.log('e');
答案:① 3 ② 4 ③ 7 ④ 8 ⑤ 5
process.nextTick(function(){
console.log(7);
});
new Promise(function(resolve){
console.log(3);
resolve();
console.log(4);
}).then(function(){
console.log(5);
});
process.nextTick(function(){
console.log(8);
})
答案:2 4 AA 5 7 9 3 1 6 8
setTimeout(()=>{
console.log(1);
},20);
console.log(2);
setTimeout(()=>{
console.log(3);
},10);
console.log(4);
console.log('AA');
for(let i=0;i<90000000;i++){
// do somthing
}
console.timeEnd('AA');
console.log(5);
setTimeout(()=>{
console.log(6)
},8);
console.log(7);
setTimeout(()=>{
console.log(8);
},15);
console.log(9);
答案:① script start ② script end ③ promise1 ④ promise2 ⑤ setTimeout
console.log('script start');
setTimeout(function(){
console.log('setTimeout');
},0);
Promise.resolve().then(function(){
console.log('promise1');
}).then(function(){
console.log('promise2');
});
console.log('script end');
答案:rejected12 rejected2undefined rejected33
new Promise((resolve,reject)=>{
reject(2)
}).then(val=>{
console.log('fulfilled 1'+val);
},val=>{
console.log('rejected 1'+val);
}).then(val=>{
console.log('fulfilled 2'+val);
return 3;
},val=>{
console.log('rejected 2'+val);
}).then(val=>{
console.log('fulfilled 3'+val);
},val=>{
console.log('rejected 3'+val);
})
答案:Promise {<pending>}
async function getData(){
return await Promise.resolve('I made it!');
}
const data = getData();
console.log(data);
this指向
答案:浏览器端:undefined node端:TypeError:this.bar is not a function(node运行时没有window的全局环境)
function foo(){
var a=2;
this.bar();
}
function bar(){
console.log(this.a)
}
foo();
答案:① 2 ② TypeError: Cannot read property 'a' of undefined ③ node端:undefined 浏览器端:2
function foo(){
console.log(this.a)
}
var a=2;
foo();
function foo(){
"use strict"
console.log(this.a);
}
var a=2;
foo();
function foo(){
console.log(this.a)
}
var a=2;
(function(){
"use strict"
foo()
})();
答案:① 2 ② 4 ③ node端:undefined 浏览器端:oops,global ④ oops,global ⑤ node端:undefined 浏览器端:oops,global
function foo(){
console.log(this.a);
}
var obj={
a:2,
foo:foo
}
obj.foo();
function foo(){
console.log(this.a);
}
var obj2={
a:4,
foo:foo
}
var obj1={
a:2,
obj2:obj2
};
obj1.obj2.foo()
function foo(){
console.log(this.a);
}
var obj={
a:2,
foo:foo
}
var bar=obj.foo;
var a='oops,global';
bar();
function foo(){
console.log(a)
}
function doFoo(fn){
fn();
}
var obj={
a:2,
foo:foo
}
var a='oops,global';
doFoo(obj.foo);
function foo(){
console.log(this.a)
}
var obj={
a:2,
foo:foo
}
var a='oops,global';
setTimeout(obj.foo,100);
答案:只有浏览器端:2 2 2
function foo(){
console.log(this.a)
}
var obj={
a:2
}
var bar=function(){
foo.call(obj)
}
bar();
setTimeout(bar,100);
bar.call(window);
答案:3 2
function foo(){
console.log(this.a)
}
var a=2;
var o={a:3,foo:foo};
var p={a:4};
o.foo();
(p.foo=o.foo)();
答案:2
function foo(){
return (a)=>{
console.log(this.a)
}
}
var obj1={
a:2
}
var obj2={
a:3
}
var bar=foo.call(obj1);
bar.call(obj2)
答案:① 20 ② 浏览器端:10 node端:undefined
var a=10;
var obj={
a:20,
intr:function(){
var a=30;
console.log(this.a);
}
}
obj.intr();
var intr=obj.intr;
intr();
答案:① 浏览器端:10 node端:undefined ② 2
var length=10;
function fn(){
console.log(this.length);
}
var obj={
length:5,
method:function(fn){
fn();
arguments\[0\]();
}
};
obj.method(fn,1);
答案:division element
function JSClass(){
this.m\_Text='division element';
this.m\_Element=document.createElement('div');
this.m\_Element.innerHTML=this.m\_Text;
this.m\_Element.addEventListener('click',this.func);
}
JSClass.prototype.Render=function(){
document.body.appendChild(this.m\_Element);
}
JSClass.prototype.func=function(){
alert(this.m\_Text);
}
var jc=new JSClass();
jc.Render();
jc.func();
答案:① 1 ② 1 0 1
var x=0;
var foo={
x:1,
bar:function(){
console.log(this.x);
var that=this;
return function(){
console.log(this.x);
console.log(that.x);
}
}
}
foo.bar();
foo.bar()();
答案:① Lydia is 21 ② function
const person={name:'Lydia'};
function sayHi(age){
console.log(\`${this.name} is ${age}\`);
}
sayHi.call(person,21);
sayHi.bind(person,21);
数组相关
答案:3
var myArray=\['foo',42,'bar'\];
myArray.baz='baz';
console.log(myArray.length);
答案:[undefined,undefined,undefined]
\[1,2,3\].map(num=>{
if(typeof num === 'number') return;
return num\*2;
})
去掉数组中非数字字符,并给每个数组+1,var arr=[1,2,3,'a',4,'b']
for(let i=arr.length-1;i>=0;i--){
if(typeof arr\[i\] === 'number'){
arr\[i\]++
}else{
arr.splice(i,1)
}
}
在两个排好序的数组中,高效率的找出相同的元素,放入新数组,var arr1=[1,3,5,7,9,12,37,45];var arr2=[2,4,9,13,45,88,92]
for(let i=0,j=0,result=\[\];i<arr1.length && j<arr2.length;){
if(arr\[i\]<arr2\[j\]){
i++;
}else if(arr1\[i\]>arr2\[j\]){
j++;
}else{
result.push(arr1\[i\]);
i++;
j++;
}
}
找出一个排好序的数组中,两个元素相加和为19的元素组合,考虑程序的执行效率,var arr=[1,2,4,6,7,11,12,15,17]
for(let i=0,j=arr.length-1;i<j;){
if(arr\[i\]+arr\[j\]>19){
j--;
}else if(arr\[i\]+arr\[j\]<19){
i++
}else{
console.log(\`${arr\[i\]}+${arr\[j\]}\`);
i++;
j--;
}
}
答案:[1,2,0,1,2,3]
\[\[0,1\],\[2,3\]\].reduce(
(acc,cur)=>{
return acc.concat(cur)
},
\[1,2\]
);
答案:['L','y','d','i','a']
console.log(\[...'Lydia'\]);
答案:undefined [10,20,30,40]
var array=\[1,2,3,4\];
var result=array.forEach(function(item,index,input){
input\[index\]=item\*10;
});
console.log(result);
console.log(array);
答案:{1,2,3,4}
const set = new Set(\[1,1,2,3,4\]);
console.log(set);
答案:1
const numbers = \[1,2,3,4,5\];
const [y] = numbers;
console.log(y);
答案:① 1 2 ② undefined 3 ③ undefined 4
[1,2,3,4].reduce((x,y)=>console.log(x,y))
答案:2
function addToList(item,list){
return list.push(item);
}
const result=addToList('apple',\['banana'\]);
console.log(result);
原型相关
答案:true true true
console.log(Object instanceof Function);
console.log(Function instanceof Object);
console.log(Function instanceof Function)
(阿里)答案:2 4 1 1 2 3 3
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();
答案:1 1 2 1 3
function A(){
alert(1);
}
function Func(){
A=function(){
alert(2);
}
return this;
}
Func.A=A;
Func.prototype={
A:()=>{
alert(3);
}
};
A();
Func.A();
Func().A();
new Func.A();
new Func().A();
答案:true
function A(){
}
function B(){
return new A();
}
A.prototype=new A();
B.prototype=new B();
var a=new A();
var b=new B();
console.log(a.\_\_proto\_\_==b.\_\_proto\_\_)
答案:Tom ['age','height','weight','hobby'] Jim ['age','height','weight','hobby']
function Person(){
this.name='Jim';
this.info=\['age','height','weight'\];
}
function Student(){};
Student.prototype=new Person();
var student1=new Student();
student1.name='Tom';
student1.info.push('hobby');
var student2=new Student();
console.log(student1.name);
console.log(student1.info);
console.log(student2.name);
console.log(student2.info);
答案:4 2 1
function Foo(){
Foo.a=function(){
console.log(1)
}
this.a=function(){
console.log(2)
}
}
Foo.prototype.a=function(){
console.log(3)
}
Foo.a=function(){
console.log(4)
}
Foo.a();
let obj=new Foo();
obj.a();
Foo.a();
答案:'Woof I am Mara',TypeError
class Dog{
constructor(name){
this.name=name;
}
}
Dog.prototype.bark=function(){
console.log(\`Woof I am ${this.name}\`);
};
const pet = new Dog('Mara');
pet.bark();
delete Dog.prototype.bark;
pet.bark();
类型相关
答案:① 18 ② 0 ③ 8 ④ 250 ⑤ 15 ⑥ 2 ⑦ '[object Object]' ⑧ 0
写出下面操作的输出结果:
① parseInt(1/0,19);
② parseInt(0.000008);
③ parseInt(0.0000008);
④ parseInt(false,16);
⑤ parseInt(function(){},16);
⑥ parseInt("103",2);
答案:7
const num=parseInt('7\*6',10);
console.log(num)
答案:全是true
判断true还是false
① false == \[\];
② "" == \[\];
③ 0 == \[\];
④ \[\] == !\[\];
⑤ 2 == \[2\];
⑥ "" == \[null\];
⑦ 0 == "\\n";
⑧ {b:42} >= {b:43};
答案:1 false
console.log(+true);
console.log(!"Lydia");
答案:
let a=3;
let b=new Number(3);
let c=3;
console.log(a==b);
console.log(a===b);
console.log(b===c);
答案:true true false
console.log(Number(2) === Number(2));
console.log(Boolean(false) === Boolean(false));
console.log(Symbol('foo') === Symbol('foo'))
作用域及闭包
答案:① 0 ② 1 ③ 2
var funs=\[\];
for(var i=0;i<3;i++){
funs\[i\]=(function(i){
return function(){
console.log(i)
}
})(i)
}
funs\[0\]();
funs\[1\]();
funs\[2\]();
答案:① 3 ② 3 ③ 3
function fun(){
for(var i=0,arr=\[\];i<3;i++){
arr\[i\]=function(){
console.log(i)
}
}
return arr;
}
var funs=fun();
funs\[0\]();
funs\[1\]();
funs\[2\]();
答案:'4'
var test=(function(i){
return function(){
alert(i\*=2);
}
})(2);
test(5);
答案:'1' '4'
var a=0,
b=0;
function A(a){
A=function(b){
alert(a+b++);
}
alert(a++);
}
A(1);
A(2);
答案:① undefined 0 0 0 ② undefined 0 1 2 ③ undefined 0 1 1
function fun(n,o){
console.log(o);
return {
fun:function(m){
return fun(m,n)
}
}
}
var a=fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b=fun(0).fun(1).fun(2).fun(3);
var c=fun(0).fun(1);
c.fun(2);
c.fun(3);
答案:7 10 16 15
var x=2;
var y={
x:3,
z:(function(x){
this.x\*=x;
x+=2;
return function(n){
this.x\*=n;
x+=3;
console.log(x);
}
})(x)
};
var m=y.z;
m(4);
y.z(5);
console.log(x,y.x);
答案:4 9 27 8 8
var number=2;
var obj={
number:4,
fn1:(function(){
this.number\*=2;
number\*=2;
var number=3;
return function(){
this.number\*=2;
number\*=3;
console.log(number);
}
})()
}
var fn1=obj.fn1;
console.log(number);
fn1();
obj.fn1();
console.log(number);
console.log(obj.number);
答案:1 undefined 2
(()=>{
let x,y;
try{
throw new Error();
}catch(x){
(x=1),(y=2);
console.log(x);
}
console.log(x);
console.log(y);
})();
答案:3
var temp=3;
function c(){
console.log(temp);
}
(function(){
var temp=4;
c();
})();
答案:undefined undefined undefined undefined undefined
for(var i=0;i<5;i++){
setTimeout(function(i){
console.log(i)
},i\*1000)
}
模块相关
答案:Error
// counter.js
let counter=10;
export default counter;
// index.js
import myCounter from './counter';
myCounter+=1;
console.log(myCounter);
答案:① running sum.js ② running index.js ③ 3
// index.js
console.log('running index.js');
import {sum} from './sum.js';
console.log(sum(1,2));
// sum.js
console.log('running sum.js');
export const sum=(a,b)=>a+b;
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。