JS从基础到进阶,看看和你的认知一样吗???
下面看几个例子,猜猜输出是什么?
提示:注意每题的标题呦!!!
1.this指向
const shape = {
radius: 10,
diameter() {
return this.radius * 2
},
perimeter: () => 2 * Math.PI * this.radius
}
shape.diameter()
shape.perimeter()
- A: 20 and 62.83185307179586
- B: 20 and NaN
- C: 20 and 63
- D: NaN and 63
2.操作符运算
console.log( +true );
console.log( !"Lydia" );
- A: 1 and false
- B: false and NaN
- C: false and false
3.语法运行
function bark() {
console.log('Woof!')
}
bark.animal = 'dog'
- A: 正常运行!
- B: SyntaxError. 你不能通过这种方式给函数增加属性。
- C: undefined
- D: ReferenceError
4.数据类型定义
const obj = { 1: 'a', 2: 'b', 3: 'c' }
const set = new Set([1, 2, 3, 4, 5])
console.log(
obj.hasOwnProperty('1'),
obj.hasOwnProperty(1),
set.has('1'),
set.has(1)
);
- A: false true false true
- B: false true true true
- C: true true false true
- D: true true true true
5.数据类型转换
const a = {};
const b = { key: 'b' };
const c = { key: 'c' };
a[b] = 123;
a[c] = 456;
console.log(a[b])
A: 123
B: 456
C: undefined
D: ReferenceError
各题答案:B、A、A、C、B,看看你都答对了吗?
补充一下,JS 全局执行上下文,做了两件事即:全局对象和 this 关键字。
这些题目的参考与出处是来自于一个非常好的github项目,如果想了解学习更多的js进阶问题,请前往JavaScript 进阶问题列表进行查看或者下载。
今天的分享到此结束,希望对您有所帮助,如果文章有错误或者有什么相关的建议,请直接私信联系我,谢谢!!!此外你如果感觉喜欢请点击进行关注,以后会不定时的分享学习心得,敬请期待(* ̄︶ ̄)(* ̄︶ ̄)(* ̄︶ ̄)
——来自一个程序猿小白的自我成长 && 学习分享