Javascript Beginner - 函数

时间:2015-04-28 19:57:36

标签: javascript

我在使用某些代码时遇到了一些麻烦。我想要它说:

  

aaa bbb ccc ddd

但相反它说:

  

未定义的bbb未定义的ddd

如何使用在函数aaa之外定义的test3来完成这项工作?

var test1;
var test2;
var test3 = test1 + ' bbb ' + test2 + ' ddd';

var aaa = function() {
  var test1 = "aaa";
  var test2 = "ccc";
  alert(test3);
}

aaa();

5 个答案:

答案 0 :(得分:1)

在使用test1test2赋值给test3之前,您需要指定test3test1的值。否则,在您执行分配时它们是未定义的。更新test2var test3; var aaa = function() { var test1 = "aaa"; var test2 = "ccc"; test3 = test1 + ' bbb ' + test2 + ' ddd' alert(test3); } aaa(); 时,test3不会动态更新。

var test3 = function (test1, test2) {
    return test1 + ' bbb ' + test2 + ' ddd';
};

var aaa = function () {
    alert(test3('aaa', 'bbb'));
};

aaa();  

如果您需要#include<stdio.h> #include<fcntl.h> #include<stdlib.h> #include<string.h> typedef struct buffer { int pid; char *buffer; } my_buffer; /* void removefifo() { unlink("fifo_server"); unlink("fifo_client"); } */ main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "usa: %s text \n", argv[0]); return EXIT_FAILURE; } FILE *file1; int fifo_server,fifo_client; char *buf_in; buf_in = malloc(20*sizeof(char)); //send PID and content my_buffer * buf_out; buf_out = (my_buffer *)malloc(sizeof(my_buffer)); buf_out->pid = getpid(); buf_out->buffer = (char *) calloc(20,sizeof(char)); strcpy(buf_out->buffer,argv[1]); fifo_server=open("fifo_server",O_RDWR); if(fifo_server < 0) { printf("Error in opening file"); exit(-1); } printf("buffer has PID %d and content %s \n",buf_out->pid,buf_out->buffer); write(fifo_server,buf_out,sizeof(struct buffer)); fifo_client=open("fifo_client",O_RDWR); if(fifo_client < 0) { printf("Error in opening file"); exit(-1); } read(fifo_client,buf_in,10*sizeof(char)); printf("\n * Reply from server: %s * \n",buf_in); close(fifo_server); close(fifo_client); } 来提供动态更新的值,您可以将其设为函数:

#include<stdio.h> 
#include<fcntl.h>
#include<stdlib.h>
#include<string.h>
#define FIFO_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) //0666

typedef struct buffer {
     int pid;
     char *buffer;
 } my_buffer;




int main()
{
int fifo_server,fifo_client;
char *buf_out;
int i;

int file1,file2;

my_buffer * buf_in;
buf_in = (my_buffer *)malloc(sizeof(my_buffer));
buf_in->buffer = (char *) calloc(20,sizeof(char));
buf_out = malloc(20*sizeof(char));

//create fifo-server with p-flag
file1 = mkfifo("fifo_server",FIFO_MODE); 
if(file1<0) {
 printf("FIFO_SERVER already exists \n");
 }

fifo_server = open("fifo_server",O_RDWR);
if(fifo_server<1) {
 printf("Error opening file");
 }
read(fifo_server,buf_in,sizeof(struct buffer)); //read PID and content
if(fifo_server<1) {
 printf("Error opening file");
 }

printf("pid %d \n",buf_in->pid);
printf("content %s \n",buf_in->buffer);

//write
strcpy(buf_out,buf_in->buffer);
for (i = 0; buf_in->buffer[i] != '\0'; i++) {
    buf_out[i] = toupper(buf_in->buffer[i]);
}

//create fifo-client with p-flag
file2 = mkfifo("fifo_client",FIFO_MODE); 
if(file2<0) {
 printf("FIFO_CLIENT already exists \n");
 }


fifo_client = open("fifo_client",O_RDWR);
write(fifo_client,buf_out,10*sizeof(char)); 
printf("\n Data sent to client \n");


close(fifo_server);
close(fifo_client);

}

答案 1 :(得分:0)

您需要将值重新分配给test3

一些相关的阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Declarations

答案 2 :(得分:0)

imageView并不神奇地知道test3test1已分配了值,您必须重新分配变量。

答案 3 :(得分:0)

您可以将定义留在外面,但在设置test1test2后需要设置

var test1, test2, test3;

var aaa = function() {
  test1 = "aaa";
  test2 = "ccc";
  test3 = test1 + ' bbb ' + test2 + ' ddd';

  alert(test3);
}

aaa();

答案 4 :(得分:0)

更好的解决方案是设置一个设置这些变量的函数。这将是一些初学者友好的代码:

var test1 = setVar('aaa');
var test2 = setVar('ccc');
var test3 = test1 + ' bbb ' + test2 + ' ddd';

function setVar(val) {
    alert(val);
    return val;
}