js测试题
- 原文 https://github.com/lengyuexin/javascript-questions/blob/master/README-zh_CN.md
01–考察var,let变量声明提升
function sayHi() {
console.log(name)//undefined
console.log(age)//Uncaught ReferenceError: age is not defined
var name = 'Lydia'
let age = 21
}
sayHi()
02–考察作用域和事件循环
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1)//3,3,3
}
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1)//0,1,2
}
03–考察this指向
const shape = {
radius: 10,
diameter() {
return this.radius * 2
},
perimeter: () => 2 * Math.PI * this.radius
}
shape.diameter()//20
shape.perimeter()//NaN
04–考察类型转换
+true;//1
!"Lydia";//false
05–考察计算属性
const bird = {
size: 'small'
}
const mouse = {
name: 'Mickey',
small: true
}
/*
* A: mouse.bird.size//invalid
* B: mouse[bird.size]//true
* C: mouse[bird["size"]]//true
*
*/
06–考察引用类型
let c = { greeting: 'Hey!' }
let d=c;
c.greeting = 'Hello'
console.log(d.greeting)//Hello
07–考察"“和”="区别
let a = 3
let b = new Number(3)
let c = 3
console.log(a == b)//true
console.log(a === b)//false
console.log(b === c)//false
08–考察类(static)方法和默认参数
class Chameleon {
static colorChange(newColor) {
this.newColor = newColor
return this.newColor
}
constructor( newColor = 'green' ) {
this.newColor = newColor
}
}
const freddie1 = new Chameleon( 'purple' )//Chameleon {newColor: "purple"}
const freddie2 = new Chameleon()//Chameleon {newColor: "green"}
freddie1.colorChange('orange')// Uncaught TypeError: freddie1.colorChange is not a function
Chameleon.colorChange('orange')//orange
09–考察变量的隐式声明
let greeting
greetign = {} // Typo!
console.log(greetign)//{}
10–考察"函数也是对象"
function bark() {
console.log('Woof!')
}
bark.animal = 'dog'
//程序正常运行,没毛病
11–考察构造函数和原型
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const member = new Person("Lydia", "Hallie");
Person.getFullName = function () {
return `${this.firstName} ${this.lastName}`;
}
console.log(member.getFullName());//Uncaught TypeError: member.getFullName is not a function
12–考察构造函数和普通函数中的this指向
function Person(firstName, lastName) {
this.firstName = firstName
this.lastName = lastName
}
const lydia = new Person('Lydia', 'Hallie')
const sarah = Person('Sarah', 'Smith')
console.log(lydia)//Person {firstName: "Lydia", lastName: "Hallie"}
console.log(sarah)//undefined
13–考察隐式类型转换
function sum(a, b) {
return a + b
}
console.log(sum(1, '2'))//12
14–考察前置++和后置++的区别
let number = 0
console.log(number++)//0
console.log(++number)//2
console.log(number)//2
15–考察字符串模板
function getPersonInfo(one, two, three) {
console.log(one)
console.log(two)
console.log(three)
}
const person = 'Lydia'
const age = 21
//如果使用标记模板字面量,第一个参数的值总是包含字符串的数组。其余的参数获取的是传递的表达式的值!
getPersonInfo`${person} is ${age} years old`//["", " is ", " years old"] "Lydia" 21
16–考察引用类型的比较
function checkAge(data) {
if (data === { age: 18 }) {
console.log('You are an adult!')
} else if (data == { age: 18 }) {
console.log('You are still an adult.')
} else {
console.log(`Hmm.. You don't have an age I guess`)//输出这一句
}
}
checkAge({ age: 18 })
17–考察"…语法"的使用
function getAge(...args) {
console.log(typeof args)//object
}
getAge(21)
18–考察严格模式下的声明全局变量
function getAge() {
'use strict'
age = 21
console.log(age)//Uncaught ReferenceError: age is not defined
}
getAge()
19–考察严格模式下的声明全局变量
function getAge() {
'use strict'
age = 21
console.log(age)//Uncaught ReferenceError: age is not defined
}
getAge()
20–考察对象和集合在存储数据类型上的差异
const obj = { 1: 'a', 2: 'b', 3: 'c' }
const set = new Set([1, 2, 3, 4, 5])
obj.hasOwnProperty('1')//true
obj.hasOwnProperty(1)//true
set.has('1')//false
set.has(1)//true
21–考察continue
for (let i = 1; i < 5; i++) {
if (i === 3) continue
console.log(i)//1 2 4
}
22–考察字符串化对象"[object Object]"
const a = {}
const b = { key: 'b' }
const c = { key: 'c' }
a[b] = 123
a[c] = 456
console.log(a[b])//456