push() 和 pop() 在未附加到数组时究竟做了什么?

What exactly do push() and pop() do when not attached to an array?

我绝对不擅长编码,我有一个问题。我知道当您使用 push() 或 pop() 函数时,它们分别在数组的末尾添加或删除某些内容。但是,我已经看到它们单独使用,而不是附加到阵列。有人可以解释这是做什么的以及它为什么有效吗?我正在使用 p5 库来让事情变得更容易,如果这有什么不同的话。

举个例子,不是声明一个数组然后在其上使用 push 或 pop:

var array = [];
array.push("something");

当我这样做时会发生什么:

//code up here
push();
//code in here
pop();
//code down here

在第二段代码中,我没有将 push() 或 pop() 附加到任何东西,我想知道它到底做了什么。

它们用于保存和恢复 p5.js 中的绘图状态。来自 documentation:

The push() function saves the current drawing style settings and transformations, while pop() restores these settings. Note that these functions are always used together. They allow you to change the style and transformation settings and later return to what you had.

它包括一个代码示例:

ellipse(0, 50, 33, 33); // Left circle

push(); // Start a new drawing state
strokeWeight(10);
fill(204, 153, 0);
translate(50, 0);
ellipse(0, 50, 33, 33); // Middle circle
pop(); // Restore original state

ellipse(100, 50, 33, 33); // Right circle

在 P5js 中,push()pop() 方法不应与 Array.pushArray.pop 混淆。它们只是完全按照 P5js reference | push.

中所述执行的程序

The push() function saves the current drawing style settings and transformations, while pop() restores these settings. Note that these functions are always used together. They allow you to change the style and transformation settings and later return to what you had. When a new state is started with push(), it builds on the current style and transform information.