js questions

1. What will be the output of the following code:

for (var i = 0; i < 5; i++) {
  setTimeout(function() { console.log(i); }, i * 1000 );
}

Explain your answer. How could the use of closures help here?

The code sample shown will not display the values 0, 1, 2, 3, and 4 as might be expected; rather, it will display 5, 5, 5, 5, and 5.
The reason for this is that each function executed within the loop will be executed after the entire loop has completed and all will therefore reference the last value stored in i, which was 5.
Closures can be used to prevent this problem by creating a unique scope for each iteration, storing each unique value of the variable within its scope, as follows:

for (var i = 0; i < 5; i++) {
    (function(x) {
        setTimeout(function() { console.log(x); }, x * 1000 );
    })(i);
}

This will produce the presumably desired result of logging 0, 1, 2, 3, and 4 to the console.

2. A closure

A closure is an inner function that has access to the variables in the
outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.

Here is a simple example:

var globalVar = "xyz";

(function outerFunc(outerArg) {
var outerVar = 'a';

(function innerFunc(innerArg) {
  var innerVar = 'b';
  
  console.log(
    "outerArg = " + outerArg + "\n" +
    "innerArg = " + innerArg + "\n" +
    "outerVar = " + outerVar + "\n" +
    "innerVar = " + innerVar + "\n" +
    "globalVar = " + globalVar);
  
})(456);
})(123);

In the above example, variables from innerFunc, outerFunc, and the global namespace are all in scope in the innerFunc. The above code will therefore produce the following output:

outerArg = 123
innerArg = 456
outerVar = a
innerVar = b
globalVar = xyz

3

splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
注释:该方法会改变原始数组。
splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。

push()
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
它直接修改 arrayObject,而不是创建一个新的数组。push() 方法和 pop() 方法使用数组提供的先进后出栈的功能。

reverse() 方法用于颠倒数组中元素的顺序。
该方法会改变原来的数组,而不会创建新的数组。

slice() 方法可从已有的数组中返回选定的元素。
返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。

sort() 方法用于对数组的元素进行排序。
返回值
对数组的引用。请注意,数组在原数组上进行排序,不生成副本。
sort 该方法会改变原来的数组,而不会创建新的数组。

concat() 方法用于连接两个或多个数组。
该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

4 typeof

undefined 值未定义
boolean 布尔值
string 字符串
number 数值
object 对象或 null
function 函数

4.1 undefined

只有一个值
对未初始化的变量执行 typeof 操作符会返回 undefined 值,而对未声明的变量执行typeof 操作符也会返回 undefined 值

var message;
alert(typeof message);// undefined
alert(typeof age); // undefined

这个结果有其逻辑上的合理性,无论对哪种变量也不可能执行真正的操作。

4.2 Null

第二个只有一个值的数据类型。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,850评论 19 139
  • 个人笔记,方便自己查阅使用 Contents Java LangAssignment, ReferenceData...
    freenik阅读 5,244评论 0 6
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,356评论 0 33
  • 功能结构图 登录界面 企业报文校验(企业校验数据报文的正确性,以保证报文数据能正常被海关系统接收。) 导入电子订单...
    零一间阅读 4,993评论 2 1
  • 地点:蒂拉斯波尔 场次:2017-2018赛季欧冠联赛资格赛第三轮 预计天气:32度 晴 谢里夫惊险晋级 谢里夫来...
    a207f83a81d0阅读 1,886评论 0 0

友情链接更多精彩内容