数组长度 [英] array length

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

问题描述

  VAR A = [1,4,5];
变种E = a.length--;

下面电子变量将包含5,但是,如果我做的:

 变种E = a.length- = 1;

下面电子将包含2个元素的数组数。
结果所以首先是一个语言提示,以模拟弹出()阵列方法是什么?

在语言的语法做的:

  A--

  A- = 1

在语义上是相同的。


解决方案

  VAR一个= [1,4,5]。
变种E = a.length--;

是相同的

  VAR一个= [1,4,5]。
变种E =则为a.length;
则为a.length =则为a.length - 1;

,而

  VAR一个= [1,4,5]。
 变种E =则为a.length - = 1;

是相同的

  VAR一个= [1,4,5]。
则为a.length =则为a.length - 1;
变种E =则为a.length;

  VAR一个= [1,4,5]。
变种E = --a.length;

在换句话说:减小而--x(且x - = 1)前x--返回值返回递减后的值。所有这些code片段弹出最后一个元素关闭阵列。

请注意,在第一个例子中, E =则为a.length - ,E的值将为3,而不是5,因此它的 a.pop()

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

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

var e = a.length-=1;

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

In the language syntax doing:

a--

or

a-=1

is semantically the same.

解决方案

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

is the same as

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

whereas

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

is the same as

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

and

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

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.

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().

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

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