小编hir*_*shi的帖子

如何使用 JavaScript 异步链接函数?

我被要求创建一个名为 的对象foo,它可以链接函数logwait

例如:

foo.log('breakfast').wait(3000).log('lunch').wait(3000).log('dinner');
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它首先打印breakfast,等待 3 秒,打印lunch,然后在 3 秒后打印dinner

我尝试过类似的方法,但它不起作用。我错过了什么?

var foo = {
  log: function(text){
    console.log(text);
    return foo;
  },

  wait: function(time) {
    setTimeout(function() {
      return foo;
    }, time);
  }
}

foo.log('breakfast').wait(3000).log('lunch').wait(3000).log('dinner');
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous

25
推荐指数
2
解决办法
1311
查看次数

为什么减少数组的长度会使最后一个元素未定义但长度不会改变?

您好,我是 javascript 新手。

我试图将数组的长度减少 1。

我这样做了,最后一个元素是undefined现在,但长度仍然是5。

var arr = [1, 2, 3, 4, 5];
arr.length = arr.length--;

console.log(arr);
console.log(arr.length);
Run Code Online (Sandbox Code Playgroud)

我糊涂了。而不是[1, 2, 3, 4][1, 2, 3, 4, 5]。为什么它给我[1, 2, 3, 4, undefined]

有人可以解释一下吗?谢谢

javascript arrays

2
推荐指数
1
解决办法
77
查看次数

标签 统计

javascript ×2

arrays ×1

asynchronous ×1