0、导言

ES6中新增了不少的新特性,来点测试题热热身。具体题目来源请看:http://perfectionkills.com/javascript-quiz-es6/

以下将一题一题来解析what和why。

1、题目一

(function(x, f = () => x) {
var x;
var y = x;
x = 2;
return [x, y, f()];
})(1) A、 [2, 1, 1]
B、 [2, undefined, 1]
C、 [2, 1, 2]
D、 [2, undefined, 2]

解析:本题主要考察的知识点是1、参数值与函数体内定义的重名变量的优先级;2、ES6的默认参数;3、箭头函数。 在本题中,先执行x的定义,然后函数参数x=1,接着是y = x = 1,接着再x = 2,第三个是执行f函数,箭头函数如果只是表达式,那么等价于return 表达式,由于箭头函数的作用域等于定义时的作用域,那么函数定义时x=1,所以最后的return x 等价于 return 1

2、题目二

(function() {
return [
(() => this.x).bind({ x: 'inner' })(),
(() => this.x)()
]
}).call({ x: 'outer' }); A、 ['inner', 'outer']
B、 ['outer', 'outer']
C、 [undefined, undefined]
D、 Error

解析:本题主要考察的是箭头函数的作用域问题,箭头函数的作用域等于定义时的作用域,所以通过bind设置的this是无效的。那么结果就显而易见了。

3、题目三

let x, { x: y = 1 } = { x }; y;

A、 undefined
B、 1
C、 { x: 1 }
D、 Error

解析:本题主要考察的是对象赋值,先定义x,然后在赋值的时候会执行一次y=1,最后返回y的值。

4、题目四

(function() {
let f = this ? class g { } : class h { };
return [
typeof f,
typeof h
];
})(); A、 ["function", "undefined"]
B、 ["function", "function"]
C、 ["undefined", "undefined"]
D、 Error

解析:本题主要考察定义函数变量时,命名函数的名称作用域问题。在定义函数变量时,函数名称只能在函数体中生效。

5、题目五

(typeof (new (class { class () {} })))

A、 "function"
B、 "object"
C、 "undefined"
D、 Error

解析:本题主要考察对象的类型,和原型方法。该提可以分解如下:

// 定义包含class原型方法的类。
var Test = class{
class(){}
};
var test = new Test(); //定义类的实例
typeof test; //出结果

6、题目六

typeof (new (class F extends (String, Array) { })).substring

A、 "function"
B、 "object"
C、 "undefined"
D、 Error

解析:本题主要考察ES6中class的继承,以及表达式的返回值和undefined的类型。题目其实可以按照如下方式分解:

//由于JS的class没有多继承的概念,所以括号被当做表达式来看
(String, Array) //Array,返回最后一个值
(class F extends Array); //class F继承成Array
(new (class F extends Array)); //创建一个F的实例
(new (class F extends (String, Array) { })).substring; //取实例的substring方法,由于没有继承String,Array没有substring方法,那么返回值为undefined
typeof (new (class F extends (String, Array) { })).substring; //对undefined取typeof

7、题目七

[...[...'...']].length

A、 1
B、 3
C、 6
D、 Error

解析:本题主要考察的是扩展运算符...的作用。扩展运算符是将后面的对象转换为数组,具体用法是:

[...<数据>] 比如 [...'abc']等价于["a", "b", "c"]

8、题目八

typeof (function* f() { yield f })().next().next()

A、 "function"
B、 "generator"
C、 "object"
D、 Error

解析:本题主要考察ES6的生成器。题目可以如下分解:

function* f() { yield f }; //定义一个生成器
var g = f(); //执行生成器
var temp = g.next(); //返回第一次yield的值
console.log(temp); //测试,查看temp,其实是一个object
temp.next();//对对象调用next方法,无效

9、题目九

typeof (new class f() { [f]() { }, f: { } })[`${f}`]

A、 "function"
B、 "undefined"
C、 "object"
D、 Error

解析:本题主要考察ES6的class,以及动态属性和模板字符串等。 实际上这个题动态属性和模板字符串都是烟雾弹,在执行new class f()的时候,就已经有语法错误了。

10、题目十

typeof `${{Object}}`.prototype

A、 "function"
B、 "undefined"
C、 "object"
D、 Error

解析:本题考察的知识点相对单一,就是模板字符串。分解如下:

var o = {Object},
str = `${o}`;
typeof str.prototype;

11、题目十一

((...x, xs)=>x)(1,2,3)

A、 1
B、 3
C、 [1,2,3]
D、 Error

解析:本题主要考察的是Rest参数的用法,在ES6中,Rest参数只能放在末尾,所以该用法的错误的。

12、题目十二

let arr = [ ];
for (let { x = 2, y } of [{ x: 1 }, 2, { y }]) {
arr.push(x, y);
}
arr; A、 [2, { x: 1 }, 2, 2, 2, { y }]
B、 [{ x: 1 }, 2, { y }]
C、 [1, undefined, 2, undefined, 2, undefined]
D、 Error

解析:本题看起来是考察let的作用域和of迭代的用法。实则是考察let的语法,let之后是一个参数名称。所以,语法错误

13、题目十三

(function() {
if (false) {
let f = { g() => 1 };
}
return typeof f;
})() A、 "function"
B、 "undefined"
C、 "object"
D、 Error

解析:本题非常有迷惑性,看似考察的let的作用域问题,实则考察了箭头函数的语法问题。

14、题目答案

相信大家看过题目的解析,对题目答案已经了然。为了完善本文,还是在最后贴出所有题目的答案:

ABBAB CBDDB DDD

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

ES6入门系列四(测试题分析)的更多相关文章

  1. C语言高速入门系列(四)

    C语言高速入门系列(四) C语言数组 ---------转载请注明出处:coder-pig 贴心小提示:假设图看不清晰可右键另存为,应该就非常清晰了; 注意上面的代码都要自己过一遍哦! 本节引言: 经 ...

  2. [转]C# 互操作性入门系列(四):在C# 中调用COM组件

    传送门 C#互操作系列文章: C# 互操作性入门系列(一):C#中互操作性介绍 C# 互操作性入门系列(二):使用平台调用调用Win32 函数 C# 互操作性入门系列(三):平台调用中的数据封送处理 ...

  3. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  4. Go语言入门系列(四)之map的使用

    本系列前面的文章: Go语言入门系列(一)之Go的安装和使用 Go语言入门系列(二)之基础语法总结 Go语言入门系列(三)之数组和切片 1. 声明 map是一种映射,可以将键(key)映射到值(val ...

  5. ES6入门系列三(特性总览下)

    0.导言 最近从coffee切换到js,代码量一下子变大了不少,也多了些许陌生感.为了在JS代码中,更合理的使用ES6的新特性,特在此对ES6的特性做一个简单的总览. 1.模块(Module) --C ...

  6. 【转载】 mybatis入门系列四之动态SQL

    mybatis 详解(五)------动态SQL 目录 1.动态SQL:if 语句 2.动态SQL:if+where 语句 3.动态SQL:if+set 语句 4.动态SQL:choose(when, ...

  7. ES6 入门系列 (一)ES6的前世今生

    要学好javascript , ECMAScript标准比什么都强, ESMAScript标准已经用最严谨的语言和最完美的角度展现了语言的实质和特性. 理解语言的本质后,你已经从沙堆里挑出了珍珠,能经 ...

  8. ES6入门系列一(基础)

    1.let命令 Tips: 块级作用域(只在当前块中有效) 不会变量提升(必须先申明在使用) 让变量独占该块,不再受外部影响 不允许重复声明 总之:let更像我们熟知的静态语言的的变量声明指令 ES6 ...

  9. ES6 入门系列 - 函数的扩展

    1函数参数的默认值 基本用法 在ES6之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log( ...

随机推荐

  1. Kinect v2.0 for windows开发环境说明

    官方文档里是这些: Supported Operating Systems and Architectures The following operating systems and architec ...

  2. [转]Navicat for MySQL快捷键

    Navicat for MySQL快捷键 ctrl+q 打开查询窗口 ctrl+/ 注释sql语句 ctrl+shift +/ 解除注释 ctrl+r 运行查询窗口的sql语句 ctrl+shift+ ...

  3. Python 深入理解yield

    只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: for 取出alist的每一项,然后把i + 1塞进去.然后通过调用取出每一项: = [1, 2, 3, 4]for x in ...

  4. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  5. ENode 2.0 - 整体架构介绍

    前言 今天是个开心的日子,又是周末,可以轻轻松松的写写文章了.去年,我写了ENode 1.0版本,那时我也写了一个分析系列.经过了大半年的时间,我对第一个版本做了很多架构上的改进,最重要的就是让ENo ...

  6. hadoop 笔记(hbase)

    hbase 基础: hbase是基于列的数据,其数据模式如下: 1.安装 1.1)hbase安装分为单机.伪分布式.分布式,单机下安装不依赖于hadoop:因为不需要分布式文件系统支持: 1.2)安装 ...

  7. 设计模式之美:Dynamic Property(动态属性)

    索引 别名 意图 结构 参与者 适用性 效果 实现 实现方式(一):Dynamic Property 的示例实现. 别名 Property Properties Property List 意图 使对 ...

  8. 如何通过自定义注解实现AOP切点定义

    面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...

  9. Silverlight无法启动调试,错误“Unable to start debugging. The Silverlight Developer Runtime is not installed. Please install a matching version.” 解决办法

    今天调试Silverlight出现了以下错误: 意思是“无法启动调试,因为Silverlight Developer Runtime没有安装,请安装一个匹配的版本”.但是按Ctrl + F5可以调试运 ...

  10. http学习笔记(四)——HTTP报文

    http报文是在http应用程序之间发送的数据块,这些数据块以一些文本形式的元信息. 请求报文从客户端流入服务器,向服务器请求数据,服务器响应请求,响应报文从服务器流出,回到客户端. 这就构成了一个事 ...