JQuery Ajax和对象字段

JQuery Ajax和对象字段,jquery,ajax,object,Jquery,Ajax,Object,我有以下代码: MyObject = function(){ this.field = null; this.build = function(){ var my_url = "..."; var my_data = "..."; $.get(my_url, my_data, function(result){ this.field = result; }); } this.

我有以下代码:

MyObject = function(){
    this.field = null;

    this.build = function(){
        var my_url = "...";
        var my_data = "...";
        $.get(my_url, my_data, function(result){
           this.field = result;
        });
    }

    this.get = function(){
        return this.field;
    }
}

object = new MyObject();
object.build();
my_result = object.get() 
my_result
现在是
null
,因为当执行内部函数
时,此
不是我的对象。

那么,如何使用
$设置
此.field
。获取返回值???

在构造函数顶部,执行以下操作:

var self = this;
这将创建一个引用当前对象的新范围变量

然后在AJAX回调中执行以下操作:

self.field = result;

在构造函数的顶部,执行以下操作:

var self = this;
这将创建一个引用当前对象的新范围变量

然后在AJAX回调中执行以下操作:

self.field = result;
我建议您将回调传递给构建函数,以便在设置字段时立即收到通知

另外,在方法中放置
var self=this
,可以确保调用
object.build.call(scope,f)
,而不会将其绑定到错误的作用域

当您想要调用另一个类的方法时,此模式在继承中特别有用

我建议您将回调传递给构建函数,以便在设置字段时立即收到通知

另外,在方法中放置
var self=this
,可以确保调用
object.build.call(scope,f)
,而不会将其绑定到错误的作用域


当您想调用另一个类的方法时,此模式在继承中特别有用。

以后,请尝试使用模块模式:

以下是模块模式:

<script type="text/javascript">
    var myNamespace = (function($) {
        var publicInstances = {};

        // ***********************
        // myObject
        publicInstances.myObject = myObject;
        function myObject() {

            /// <summary>A pointer to this</summary>
            var self = this;

            this.someProperty = new String();

            this.initialize = function() {
                /// your code here
            }
            this.someMethod = function() {
                /// your code here
            }

            self.initialize();
        }

        return publicInstances;
    })(jQuery);


    jQuery(document).ready(function() {
        // Use would look like
        var myInstance = new myNamespace.myObject();
    });
</script>

var myNamespace=(函数($){
var publicInstances={};
// ***********************
//肌体
publicInstances.myObject=myObject;
函数myObject(){
///指向此的指针
var self=这个;
this.someProperty=新字符串();
this.initialize=函数(){
///你的代码在这里
}
this.someMethod=函数(){
///你的代码在这里
}
self.initialize();
}
返回公共实例;
})(jQuery);
jQuery(文档).ready(函数(){
//使用看起来像
var myInstance=new myNamespace.myObject();
});
以下是对象的外观:

<script type="text/javascript">
    var myNamespace = (function($) {
        var publicInstances = {};

        // ***********************
        // myObject
        publicInstances.myObject = myObject;
        function myObject() {

            /// <summary>A pointer to this</summary>
            var self = this;

            this.someProperty = new String();

            this.initialize = function() {
                /// your code here
            }
            this.someMethod = function() {
                /// your code here
            }

            self.initialize();
        }

        return publicInstances;
    })(jQuery);


    jQuery(document).ready(function() {
        // Use would look like
        var myInstance = new myNamespace.myObject();
    });
</script>
var myNamespace=(函数($)
{
var publicInstances={};
// ***********************
//肌体
publicInstances.myObject=myObject;
函数myObject(url、数据){
///指向此的指针
var self=这个;
///包含结果
this.field=null;//改为命名此结果。
this.url=url;
这个数据=数据;
this.initialize=函数(){
///初始化任何东西
}
this.build=function(){
$.get(self.url、self.data、函数(结果){
self.field=结果;
cb(自我场);
});
}
this.cb=函数(结果){
///做任何事
}
self.initialize();
}
返回公共实例;
})(jQuery);

以后,请尝试使用模块模式:

以下是模块模式:

<script type="text/javascript">
    var myNamespace = (function($) {
        var publicInstances = {};

        // ***********************
        // myObject
        publicInstances.myObject = myObject;
        function myObject() {

            /// <summary>A pointer to this</summary>
            var self = this;

            this.someProperty = new String();

            this.initialize = function() {
                /// your code here
            }
            this.someMethod = function() {
                /// your code here
            }

            self.initialize();
        }

        return publicInstances;
    })(jQuery);


    jQuery(document).ready(function() {
        // Use would look like
        var myInstance = new myNamespace.myObject();
    });
</script>

var myNamespace=(函数($){
var publicInstances={};
// ***********************
//肌体
publicInstances.myObject=myObject;
函数myObject(){
///指向此的指针
var self=这个;
this.someProperty=新字符串();
this.initialize=函数(){
///你的代码在这里
}
this.someMethod=函数(){
///你的代码在这里
}
self.initialize();
}
返回公共实例;
})(jQuery);
jQuery(文档).ready(函数(){
//使用看起来像
var myInstance=new myNamespace.myObject();
});
以下是对象的外观:

<script type="text/javascript">
    var myNamespace = (function($) {
        var publicInstances = {};

        // ***********************
        // myObject
        publicInstances.myObject = myObject;
        function myObject() {

            /// <summary>A pointer to this</summary>
            var self = this;

            this.someProperty = new String();

            this.initialize = function() {
                /// your code here
            }
            this.someMethod = function() {
                /// your code here
            }

            self.initialize();
        }

        return publicInstances;
    })(jQuery);


    jQuery(document).ready(function() {
        // Use would look like
        var myInstance = new myNamespace.myObject();
    });
</script>
var myNamespace=(函数($)
{
var publicInstances={};
// ***********************
//肌体
publicInstances.myObject=myObject;
函数myObject(url、数据){
///指向此的指针
var self=这个;
///包含结果
this.field=null;//改为命名此结果。
this.url=url;
这个数据=数据;
this.initialize=函数(){
///初始化任何东西
}
this.build=function(){
$.get(self.url、self.data、函数(结果){
self.field=结果;
cb(自我场);
});
}
this.cb=函数(结果){
///做任何事
}
self.initialize();
}
返回公共实例;
})(jQuery);

请不要调用您的对象
对象
@Alnitak好的,我会调用MyObject…请不要调用您的对象
对象
@Alnitak好的,我将调用MyObject…我建议在方法
this.build
中执行此操作,这样
object.build.apply(范围,…)
不会中断。实际上,如果有关系,我可能会同时执行这两个操作。我建议在方法
this.build
中执行此操作,以便
object.build.apply(范围,…)
不会中断。在实践中,如果重要的话,我可能会同时做这两件事。请详细说明范围问题-您是说您可能特别希望使用不同于构造对象本身的范围吗?@Alnitak您可能希望重用它。这是一个OOP模式。请详细说明范围问题-您是说您可能特别希望使用与构造对象本身不同的范围吗?@Alnitak您可能希望重用它。这是一种面向对象的模式。这种模式设计得有点过度,不是很好。仍在全球范围内泄漏。而且,您不会向异步加载程序开放自己。但它比临时编码更好!我不明白你的意思