从其对象外部调用javascript函数

时间:2022-03-28 16:56:50

I have some simple javascript that as far as i can tell should work but doesn't.

我有一些简单的JavaScript,据我所知应该工作,但没有。

The code is below

代码如下

var presenter = new Practicum.Web.TEI.StudentPlacement2009.CreateLetter_class(); //this is a class generated by Ajax.Net

function GetLetters() {
    var GetLettersParams = new Object();
    GetLettersParams.TemplateType = $('#LetterTypes').val();
    var letters = ajaxCall(presenter.GetLetters, GetLettersParams);
    createOptions('Templates', letters, 'Id', 'Name', true);
}

function ajaxCall(ajaxMethod, parameters) {
    var response = ajaxMethod.call(parameters); //fails here with the message in
    if (response.error != null) {
        alert('An error has occured\r\n' + response.error.Message);
        return;
    }
    return response.value;
}

this is part of the class Ajax.Net produces.

这是Ajax.Net产生的类的一部分。

Practicum.Web.TEI.StudentPlacement2009.CreateLetter_class = function() {};
Object.extend(Practicum.Web.TEI.StudentPlacement2009.CreateLetter_class.prototype, Object.extend(new AjaxPro.AjaxClass(), {
GetLetterTypes: function() {
return this.invoke("GetLetterTypes", {}, this.GetLetterTypes.getArguments().slice(0));
},
GetDegrees: function() {
return this.invoke("GetDegrees", {}, this.GetDegrees.getArguments().slice(0));
},
GetLetters: function(getLettersParams) {
return this.invoke("GetLetters", {"getLettersParams":getLettersParams}, this.GetLetters.getArguments().slice(1));
} ...

Any help would be much appriciated; Colin G

任何帮助都会很有帮助;科林G.

3 个解决方案

#1


The first parameter that needs to be passed to Function.call() is the object on which the function is called. Then follow the function parameters as separate values:

需要传递给Function.call()的第一个参数是调用该函数的对象。然后按功能参数作为单独的值:

func.call(someobj, param1, param2, ...);

To call a function with an array of arguments you should use apply(). apply() also takes the object for which the method should be called as first parameter:

要使用参数数组调用函数,您应该使用apply()。 apply()还将应该调用该方法的对象作为第一个参数:

func.apply(someobj, params);

So in your case it would look something like this:

所以在你的情况下,它看起来像这样:

function ajaxCall(ajaxMethod, obj, parameters) {
  var response = ajaxMethod.call(obj, parameters);
  // ...
}

var letters = ajaxCall(presenter.GetLetters, presenter, GetLettersParams);

#2


You need to pass an object to the first argument of the call method e.g.:

你需要将一个对象传递给调用方法的第一个参数,例如:

ajaxMethod.call(presenter, parameters);

See http://www.webreference.com/js/column26/call.html

#3


Supertux is right. You could try this to make sure the context is set for "call":

Supertux是对的。您可以尝试这样做以确保为“call”设置上下文:

function GetLetters() {
    var GetLettersParams = new Object();
    GetLettersParams.TemplateType = $('#LetterTypes').val();
    var letters = ajaxCall(presenter.GetLetters, presenter, GetLettersParams);
    createOptions('Templates', letters, 'Id', 'Name', true);
}

function ajaxCall(ajaxMethod, context, parameters) {
    var response = ajaxMethod.call(context, parameters); //Call requires a context
    if (response.error != null) {
        alert('An error has occured\r\n' + response.error.Message);
        return;
    }
    return response.value;
}

Or you could simplify things quite a bit by not using ajaxCall:

或者你可以通过不使用ajaxCall来简化一些事情:

function GetLetters() {
    var GetLettersParams = { 
            TemplateType: $('#LetterTypes').val() 
        },
        response = presenter.GetLetters(GetLettersParams);

    if (response.error != null) {
        alert('An error has occured\r\n' + response.error.Message);
        return;
    }

    createOptions('Templates', response.value, 'Id', 'Name', true);
}

#1


The first parameter that needs to be passed to Function.call() is the object on which the function is called. Then follow the function parameters as separate values:

需要传递给Function.call()的第一个参数是调用该函数的对象。然后按功能参数作为单独的值:

func.call(someobj, param1, param2, ...);

To call a function with an array of arguments you should use apply(). apply() also takes the object for which the method should be called as first parameter:

要使用参数数组调用函数,您应该使用apply()。 apply()还将应该调用该方法的对象作为第一个参数:

func.apply(someobj, params);

So in your case it would look something like this:

所以在你的情况下,它看起来像这样:

function ajaxCall(ajaxMethod, obj, parameters) {
  var response = ajaxMethod.call(obj, parameters);
  // ...
}

var letters = ajaxCall(presenter.GetLetters, presenter, GetLettersParams);

#2


You need to pass an object to the first argument of the call method e.g.:

你需要将一个对象传递给调用方法的第一个参数,例如:

ajaxMethod.call(presenter, parameters);

See http://www.webreference.com/js/column26/call.html

#3


Supertux is right. You could try this to make sure the context is set for "call":

Supertux是对的。您可以尝试这样做以确保为“call”设置上下文:

function GetLetters() {
    var GetLettersParams = new Object();
    GetLettersParams.TemplateType = $('#LetterTypes').val();
    var letters = ajaxCall(presenter.GetLetters, presenter, GetLettersParams);
    createOptions('Templates', letters, 'Id', 'Name', true);
}

function ajaxCall(ajaxMethod, context, parameters) {
    var response = ajaxMethod.call(context, parameters); //Call requires a context
    if (response.error != null) {
        alert('An error has occured\r\n' + response.error.Message);
        return;
    }
    return response.value;
}

Or you could simplify things quite a bit by not using ajaxCall:

或者你可以通过不使用ajaxCall来简化一些事情:

function GetLetters() {
    var GetLettersParams = { 
            TemplateType: $('#LetterTypes').val() 
        },
        response = presenter.GetLetters(GetLettersParams);

    if (response.error != null) {
        alert('An error has occured\r\n' + response.error.Message);
        return;
    }

    createOptions('Templates', response.value, 'Id', 'Name', true);
}