如何使用另一个函数为javascript对象赋值?
我实例化了一个名为vacationDayRequested的javascript对象如何使用另一个函数为javascript对象赋值?,javascript,ajax,Javascript,Ajax,我实例化了一个名为vacationDayRequested的javascript对象 function vacationDayRequested(_requestedDate,_clockNo) { this.status = "unknowm"; this.ClockNo = _clockNo; this.DayRequested = _requestedDate; this.Selectable = IsSelectableBASIC(this); }
function vacationDayRequested(_requestedDate,_clockNo) {
this.status = "unknowm";
this.ClockNo = _clockNo;
this.DayRequested = _requestedDate;
this.Selectable = IsSelectableBASIC(this);
}
我想通过一个名为IsSelectableBASIC的ajax函数来分配Selectable属性的值:
function IsSelectableBASIC(vacationDayRequested)
{
var returnValue = false;
$.ajax({
url: "VacationRequest/IsDaySelectableBasic",
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(vacationDayRequested),
success: function (data) {
if (data.OperationMessage == "yes") {
returnValue = true;
}
else {
returnValue = false;
}
}
});
return returnValue;
}
我使用Chrome中的开发者工具检查,我的ajax返回的JSON的OperationMessage值为“yes”,而我的vacationDayRequested.Selective返回为“false”。我的javascript中是否存在设计缺陷
下面是我如何实例化对象并在客户端代码中使用它:
var vdr = new vacationDayRequested(requestedDate, clockNo);
alert(vdr.Selectable);
更新:9/11/2014
我现在对回电越来越清楚了。我在描述下面代码中发生的事情时是否正确?另外,请让我知道,如果我在整个过程中正确使用了术语回调
- 按钮按下了
- button click事件调用GetValueOfX函数,并向其传递一个名为DoSomething的回调函数
- GetValueOfX执行并具有一个ajax函数,其中success调用DoSomething,因为我们传入了DoSomething 压榨 $(“#btn”)。单击(函数(){
GetValueOfX(DoSomething);
});
function DoSomething(result)
{
***//This is the correct place for code that depends on the ajax return***
x = result;
debugger;
alert(x);
}
function GetValueOfX(callback)
{
$.ajax({
url: "/JSTesting/GetSomeTestData",
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: callback
})
***//if we had code here it might fire before the ajax request is complete***
}