questions for javascript
javascript有意思的题
questions for javascript
javascript有意思的题
相信大家一面试才会想起刷力扣
力扣官网,今天没那么麻烦, 记录一下一些有意思的题,懒得翻译了,直接看题库吧,想要了解更多,请走电梯
function sayHi() { console.log(name); console.log(age); var name = 'Lydia'; let age = 21; } sayHi();
Lydia
and undefined
Lydia
and ReferenceError
ReferenceError
and 21
undefined
and ReferenceError
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1); } for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1); }
0 1 2
and 0 1 2
0 1 2
and 3 3 3
3 3 3
and 0 1 2
const shape = { radius: 10, diameter() { return this.radius * 2; }, perimeter: () => 2 * Math.PI * this.radius, }; console.log(shape.diameter()); console.log(shape.perimeter());
20
and 62.83185307179586
20
and NaN
20
and 63
NaN
and 63
+true; !'Lydia';
1
and false
false
and NaN
false
and false
const bird = { size: 'small', }; const mouse = { name: 'Mickey', small: true, };
mouse.bird.size
is not validmouse[bird.size]
is not validmouse[bird["size"]]
is not validlet c = { greeting: 'Hey!' }; let d; d = c; c.greeting = 'Hello'; console.log(d.greeting);
Hello
Hey!
undefined
ReferenceError
TypeError
let a = 3; let b = new Number(3); let c = 3; console.log(a == b); console.log(a === b); console.log(b === c);
true
false
true
false
false
true
true
false
false
false
true
true
class Chameleon { static colorChange(newColor) { this.newColor = newColor; return this.newColor; } constructor({ newColor = 'green' } = {}) { this.newColor = newColor; } } const freddie = new Chameleon({ newColor: 'purple' }); console.log(freddie.colorChange('orange'));
orange
purple
green
TypeError
let greeting; greetign = {}; // Typo! console.log(greetign);
{}
ReferenceError: greetign is not defined
undefined
function bark() { console.log('Woof!'); } bark.animal = 'dog';
SyntaxError
. You cannot add properties to a function this way."Woof"
gets logged.ReferenceError
您的鼓励是我前进的动力---
使用微信扫描二维码完成支付