我被要求创建一个名为 的对象foo,它可以链接函数log和wait。
例如:
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 新手。
我试图将数组的长度减少 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]?
有人可以解释一下吗?谢谢