在JavaScript中动态创建变量

时间:2013-01-07 18:20:39

标签: javascript variables

我有一个脚本可以创建一个名为eventData1的变量。在事件部分中,创建了events。我想动态地完成这项工作,而不是在html文件中静态地使用它。

    var eventData1 = {
          options: {
            timeslotsPerHour: 4,
            timeslotHeight: 20,
            defaultFreeBusy: {free: true}
          },
          events : 
            {'id':1, 'start': new Date(year, month, day, 12), 'end': new Date(year, month, day, 13, 30), 'title': 'Lunch with Mike', userId: 0},
            {'id':2, 'start': new Date(year, month, day, 14), 'end': new Date(year, month, day, 14, 45), 'title': 'Dev Meeting', userId: 1},
            {'id':3, 'start': new Date(year, month, day+1, 18), 'end': new Date(year, month, day+1, 18, 45), 'title': 'Hair cut', userId: 1},
            {'id':4, 'start': new Date(year, month, day+2, 8), 'end': new Date(year, month, day+2, 9, 30), 'title': 'Team breakfast', userId: 0},
            {'id':5, 'start': new Date(year, month, day+1, 14), 'end': new Date(year, month, day+1, 15), 'title': 'Product showcase', userId: 1}
          ]
    };

所以我的文件创建了另一个名为appointments的变量,其设置与您在上面看到的完全相同:

[{'id': 25, 'start': new Date( '2013-01-07 14:45:00'), 'end': new Date('2013-01-07 15:45:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 26, 'start': new Date( '2013-01-10 11:15:00'), 'end': new Date('2013-01-10 12:15:00'), 'title': 'test appointment from javascript', userId: 0}]

如何使用appointments变量替换静态创建的事件?

我尝试了这个,但它不起作用:

    var eventData1 = {
          options: {
            timeslotsPerHour: 4,
            timeslotHeight: 20,
            defaultFreeBusy: {free: true}
          },
          events : appointments
    };

编辑:马特是对的,约会变量在范围内,但尚未设置:

  var appointments = "[";
  var counter = 0;
   $.getJSON('link', function(data) {
        console.log('entered getJSON()');
        console.log(data);
        $.each(data, function(i, appointment) {
            var id = appointment.appointmentId;
            var start = appointment.start;
            var end = appointment.end;
            var title = appointment.prename;
            var userid = appointment.workerid;

            appointments += "{'id': " + id + ", 'start': new Date( '" + start+ "'), 'end': new Date('" + end + "'), 'title': 'test appointment from javascript', userId: 0}";

            if (i === (data.length - 1)) {
                // this is the last                    
            } else {
                appointments += ",";
            }             
            counter++;
        });

        appointments += "]";

    console.log(appointments);
    });

我知道在其他代码使用它之前我可以做些什么来设置它?

1 个答案:

答案 0 :(得分:0)

我对你的文件中代码的哪一部分感到有点困惑,但我猜你是向服务器发出请求,然后设置eventData1然后处理服务器响应。

Javascript字符串是不可变的,因此当您修改appointments eventData1.events时不会更新。看看这个例子:

var str = "hi";
var obj = {myString: str}; // obj.myString = "hi"
str = "hello"; // obj.myString is still "hi"

“hi”字符串未更新,因为它是不可变的,它被新字符串覆盖。

您可以做的一件事就是明确更新eventData.events并为其分配appointments的新值。

但是因为appointments是Javascript数组的字符串表示,为什么不使用数组呢?而不是

appointments += "{'id': " + id + ", 'start': new Date( '" + start+ "'), 'end': new Date('" + end + "'), 'title': 'test appointment from javascript', userId: 0}";

使用

appointments = []; // Do this outside of the $.each
... 
appointments.push(appointments += "{'id': " + id + ", 'start': new Date( '" + start+ "'), 'end': new Date('" + end + "'), 'title': 'test appointment from javascript', userId: 0}");