即使在函数中更新后,全局对象仍未定义
Global object is undefined even after updating it in a function
作为 C++ 的用户,并且是 web-dev 的新手,我无法理解为什么全局变量 'angle' 没有在函数 setup() 中更新。因为,在我 运行 console.log(angle[1].arc_colour) 的最后一行,它 returns 我未定义。但是,如果我尝试在控制台中打印相同的命令,则会返回一个有限的数字。
(注意:setup() 是 p5.js 中的一个函数,它在每次执行时首先被调用)
//quick.js
var angle = new Array(200);
function Drawn_Arc(begin_angle, end_angle, arc_colour) {
this.begin_angle = begin_angle;
this.end_angle = end_angle;
this.arc_colour = arc_colour;
}
function swap(a, b) {
a = a + b;
b = a - b;
a = a - b;
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(230);
noStroke();
colorMode(HSL, 2000);
for (let i = 1; i <= 200; i++) {
angle[i - 1] = new Drawn_Arc(((i - 1) * PI) / 100, (i * PI) / 100, random(0, 2000));
fill(angle[i - 1].arc_colour, 2000, 1000);
arc(windowWidth / 2, windowHeight / 2, windowHeight - 12, windowHeight - 12, angle[i - 1].begin_angle, angle[i - 1].end_angle, PIE);
}
}
console.log(angle[1].arc_colour);
//---html---
<html style="margin : 0px; padding : 0px">
<head>
<meta charset="utf-8" />
<title>Quick Sort</title>
</head>
<body style="margin : 0px; padding : 0px">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script type="text/javascript" src="quick.js"></script>
</body>
</html>
[...] global variable 'angle' not get updated inside the function setup()
. Because, in the last line where I run console.log(angle[1].arc_colour)
, it returns me undefined.
行 onsole.log(angle[1].arc_colour);
不在 setup
函数中,但在全局范围内位于脚本的末尾。
将console.log(angle[1].arc_colour);
移动到设置函数中,以便在数组初始化后完成:
function setup() {
createCanvas(windowWidth, windowHeight);
background(230);
noStroke();
colorMode(HSL, 2000);
for (let i = 1; i <= 200; i++) {
angle[i - 1] = new Drawn_Arc(((i - 1) * PI) / 100, (i * PI) / 100, random(0, 2000));
fill(angle[i - 1].arc_colour, 2000, 1000);
arc(windowWidth / 2, windowHeight / 2, windowHeight - 12,
windowHeight - 12, angle[i - 1].begin_angle, angle[i - 1].end_angle, PIE);
}
console.log(angle[1].arc_colour);
}
// console.log(angle[1].arc_colour);
What if I wanted to use the array in some other function. How would I do that?
当然你也可以在函数中记录数组:
function setup() {
createCanvas(windowWidth, windowHeight);
// ......
logarray();
}
function logarray()
{
console.log(angle[1].arc_colour);
}
作为 C++ 的用户,并且是 web-dev 的新手,我无法理解为什么全局变量 'angle' 没有在函数 setup() 中更新。因为,在我 运行 console.log(angle[1].arc_colour) 的最后一行,它 returns 我未定义。但是,如果我尝试在控制台中打印相同的命令,则会返回一个有限的数字。
(注意:setup() 是 p5.js 中的一个函数,它在每次执行时首先被调用)
//quick.js
var angle = new Array(200);
function Drawn_Arc(begin_angle, end_angle, arc_colour) {
this.begin_angle = begin_angle;
this.end_angle = end_angle;
this.arc_colour = arc_colour;
}
function swap(a, b) {
a = a + b;
b = a - b;
a = a - b;
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(230);
noStroke();
colorMode(HSL, 2000);
for (let i = 1; i <= 200; i++) {
angle[i - 1] = new Drawn_Arc(((i - 1) * PI) / 100, (i * PI) / 100, random(0, 2000));
fill(angle[i - 1].arc_colour, 2000, 1000);
arc(windowWidth / 2, windowHeight / 2, windowHeight - 12, windowHeight - 12, angle[i - 1].begin_angle, angle[i - 1].end_angle, PIE);
}
}
console.log(angle[1].arc_colour);
//---html---
<html style="margin : 0px; padding : 0px">
<head>
<meta charset="utf-8" />
<title>Quick Sort</title>
</head>
<body style="margin : 0px; padding : 0px">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.js"></script>
<script type="text/javascript" src="quick.js"></script>
</body>
</html>
[...] global variable 'angle' not get updated inside the function
setup()
. Because, in the last line where I runconsole.log(angle[1].arc_colour)
, it returns me undefined.
行 onsole.log(angle[1].arc_colour);
不在 setup
函数中,但在全局范围内位于脚本的末尾。
将console.log(angle[1].arc_colour);
移动到设置函数中,以便在数组初始化后完成:
function setup() {
createCanvas(windowWidth, windowHeight);
background(230);
noStroke();
colorMode(HSL, 2000);
for (let i = 1; i <= 200; i++) {
angle[i - 1] = new Drawn_Arc(((i - 1) * PI) / 100, (i * PI) / 100, random(0, 2000));
fill(angle[i - 1].arc_colour, 2000, 1000);
arc(windowWidth / 2, windowHeight / 2, windowHeight - 12,
windowHeight - 12, angle[i - 1].begin_angle, angle[i - 1].end_angle, PIE);
}
console.log(angle[1].arc_colour);
}
// console.log(angle[1].arc_colour);
What if I wanted to use the array in some other function. How would I do that?
当然你也可以在函数中记录数组:
function setup() {
createCanvas(windowWidth, windowHeight);
// ......
logarray();
}
function logarray()
{
console.log(angle[1].arc_colour);
}