从对象外部调用 javascript 函数

发布于 2024-07-28 23:49:56 字数 1411 浏览 4 评论 0原文

我有一些简单的 javascript,据我所知应该可以工作,但不能。

下面的代码

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;
}

是 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));
} ...

任何帮助将不胜感激; 科林·G

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轻许诺言 2024-08-04 23:49:56

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

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

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

func.apply(someobj, params);

因此在您的情况下,它看起来像这样:

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

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

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:

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:

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);
蝶…霜飞 2024-08-04 23:49:56

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

ajaxMethod.call(presenter, parameters);

请参阅 http:// /www.webreference.com/js/column26/call.html

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

木槿暧夏七纪年 2024-08-04 23:49:56

超级礼服是对的。 您可以尝试这样做以确保为“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;
}

或者您可以通过不使用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);
}

Supertux is right. You could try this to make sure the context is set for "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:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文