更新:我已经弄清楚了。感谢 eol ,让我思考了承诺。虽然推荐的脚本没有让我明白,但思想却实现了。
要使对象正确传递,我需要从对象本身开始,并重组代码以支持该目的。
var barBuildX = function(csv, domainColumnName, rangeMin, rangeMax, paddingInner, paddingOuter) {
var x = {
rMin: rangeMin,
rMax: rangeMax,
padIn: paddingInner,
padOut: paddingOuter,
domain: (function() {
var dom = [];
d3.csv(csv).then(function(data) {
data.forEach(function(d) {
dom[dom.length] = d[domainColumnName];
});
})
return dom;
})(),
};
console.log(x);
return x;
};
};
这是一个相当基本的子例程,旨在用满足d3.js scaleBand()函数所需的字段填充对象。该脚本的第一部分按预期方式工作-根据提供的CSV文件中的“ domainColumnName”构建域名数组。但是,第二部分实际上是构建将要返回的“ x”对象的地方,无法正常工作-无法写入控制台或不带回我试图传递给它的任何数据。看到它特别麻烦,因为它应该只是设置属性值并返回该对象以供在其他地方使用......有什么想法吗?
var barBuildX = function(csv, domainColumnName, rangeMin, rangeMax, paddingInner, PaddingOuter) {
var dom = [];
d3.csv(csv).then(function(data) {
console.log(data); //this works
data.forEach(function(d) {
dom[dom.length] = d[domainColumnName];
});
console.log(dom); //this works
var x = {
domain: dom,
rMin: rangeMin,
rMax: rangeMax,
padIn: paddingInner,
padOut: paddingOuter
};
console.log(x); //this DOES NOT work
return x; //Undefined... / Type Error
});
};
有人注意到变量的拼写有所不同,很不幸,这是我复制到此线程时犯的一个错误。在原始来源中,可变拼写没有区别。
我在Chrome控制台中收到的错误消息是:“未捕获的TypeError:无法读取未定义的属性'domain'”
所以,返回对象以未定义的形式出现...
答案 0 :(得分:0)
您不会从函数内部返回promise,这就是为什么您将获得Bpam <- list(structure(c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0.744519, 0.7233527,
0.8563551, 0.8483707, 0.8461553, 0.836892, 0.834105, 0.8219688,
0.7046171, 0.5731629), .Dim = c(10L, 2L), .Dimnames = list(c("D.var",
"Hy.adu", "A.cra", "Cu.cir", "Sp.sp", "E.gad", "L.elo", "A.mor",
"H.com", "S.cad"), c("cluster", "sil_width"))), structure(c(1,
1, 0, 0, 0, 0, 0, 0, 0.5518385, 0.1878755, 0.8183177, 0.7964595,
0.7964595, 0.7879583, 0.7667134, 0.5821773), .Dim = c(8L, 2L), .Dimnames = list(
c("Hy.adu", "D.var", "Ab.gad", "L.elo", "A.cra", "Cu.cir",
"A.mor", "S.cad"), c("cluster", "sil_width"))))
作为返回值的原因。做这样的事情:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
//#include "include/state.h"
//#include "include/state_machine.h"
//#include "include/renderable.h"
const int WIDTH = 800;
const int HEIGHT = 600;
// called when user resizes window
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
// called when we receive input
void processInput(GLFWwindow *window) {
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, 1);
}
GLuint get_checker_texture() {
unsigned char texDat[64];
for (int i = 0; i < 64; ++i)
texDat[i] = ((i + (i / 8)) % 2) * 128 + 127;
//upload to GPU texture
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texDat);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return tex;
}
void draw_texture(GLuint texture, unsigned int vbo, unsigned int vao, unsigned int ebo) {
glBindVertexArray(vao);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
//void render_box(renderable* this, unsigned int vbo, unsigned int vao, unsigned int ebo) {
// draw_texture(this->texture, this->x, this->y, this->z, vbo, vao, ebo);
//}
int main(int argc, char* argv[]) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // only on MACOS
#endif
// creating the window
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL App", NULL, NULL);
if (window == NULL) {
printf("Failed to create GLFW window");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// hook on window resize
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
printf("Failed to initialize GLAD");
return -1;
}
printf("OpenGL %d.%d\n", GLVersion.major, GLVersion.minor);
glViewport(0, 0, WIDTH, HEIGHT);
// just making sure the state machine works
// for testing purposes only
/*state_machine* sm = init_state_machine();
state* state0 = init_state();
state* state1 = init_state();
state1->background_color->r = 1.0f;
state* state2 = init_state();
state2->background_color->g = 1.0f;
ss_vector_append(sm->states, state0);
ss_vector_append(sm->states, state1);
ss_vector_append(sm->states, state2);*/
//renderable* box = init_renderable(0.0f, 0.0f, 0.0f, &render_box);
unsigned int tex = get_checker_texture();
unsigned int vbo;
glGenBuffers(1, &vbo);
unsigned int vao;
glGenVertexArrays(1, &vao);
unsigned int ebo;
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
const char* vertex_shader_src =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"layout (location = 2) in vec2 aTexCoord;\n"
"\n"
"out vec3 ourColor;\n"
"out vec2 TexCoord;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 1.0);\n"
" ourColor = aColor;\n"
" TexCoord = aTexCoord;\n"
"}";
unsigned int vertex_shader;
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_src, NULL);
glCompileShader(vertex_shader);
int success;
char infoLog[512];
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex_shader, 512, NULL, infoLog);
printf("%s\n", infoLog);
}
const char* fragment_shader_src =
"#version 330 core\n"
"out vec4 FragColor;\n"
"\n"
"in vec3 ourColor;\n"
"in vec2 TexCoord;\n"
"\n"
"uniform sampler2D ourTexture;\n"
"\n"
"void main()\n"
"{\n"
" FragColor = texture(ourTexture, TexCoord);\n"
"}";
unsigned int fragment_shader;
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_src, NULL);
glCompileShader(fragment_shader);
int success0;
char infoLog0[512];
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success0);
if (!success0) {
glGetShaderInfoLog(fragment_shader, 512, NULL, infoLog0);
printf("%s\n", infoLog0);
}
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertex_shader);
glAttachShader(shaderProgram, fragment_shader);
glLinkProgram(shaderProgram);
int success1;
char infoLog1[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success1);
if(!success1) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog1);
printf("%s\n", infoLog1);
}
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// render loop
while(!glfwWindowShouldClose(window)) {
processInput(window);
//state* current_state = ((state*)sm->states->items[sm->stateindex]);
glClear(GL_COLOR_BUFFER_BIT);
// render here
glClearColor(
0.0f,//current_state->background_color->r,
0.0f,//current_state->background_color->g,
0.0f,//current_state->background_color->b,
0.0f//current_state->background_color->a
);
glUseProgram(shaderProgram);
draw_texture(tex, vbo, vao, ebo);
// for testing purposes only
//sm->stateindex = sm->stateindex < 2 ? (sm->stateindex + 1) : 0;
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}