JS中的数组长度 [英] array length in JS

查看:53
本文介绍了JS中的数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var a = [1,4,5];
var e = a.length--;

此处 e 变量将包含5.但是如果我这样做:

Here e variable will contain 5. But if I do:

var e = a.length-=1;

此处 e 将包含2个元素数组.
那么第一个是模拟pop()数组方法的语言技巧"吗?

Here e will contain 2 the number of elements array.
So the first is a language 'tip' to simulate a pop() array methods?

在语言语法中:

a--

a-=1

在语义上是相同的.

推荐答案

var a = [1, 4, 5];
var e = a.length--;

var a = [1, 4, 5];
var e = a.length;
a.length = a.length - 1;

 var a = [1, 4, 5];
 var e = a.length -= 1;

var a = [1, 4, 5];
a.length = a.length - 1;
var e = a.length;

var a = [1, 4, 5];
var e = --a.length;

换句话说:x--返回减小之前的值,而--x(和x-= 1)返回减小之后的值.所有这些代码段都将数组a中的最后一个元素弹出.

In other words: x-- returns the value before decreasing whereas --x (and x -= 1) returns the value after decreasing. All these code snippets pop the last element off the array a.

请注意,在第一个示例中, e = a.length-,e的值为3,而不是5,因此 not a.pop().

Note that in the first example, e = a.length--, e will have the value 3, not 5, therefore it is not the same as a.pop().

这篇关于JS中的数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆