jQuery Ajax和对象字段

I have this code:

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 now is null because when the inner function is executed this is not my object.
So, how can i set this.field with the $.get returned value???

In the top of your constructor, do:

var self = this;

this creates a new scoped variable that refers to the current object.

Then in the AJAX call back do:

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

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

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

object = new MyObject();
object.build(function(field) {
    console.log(object.get() === field);
});

I recommend you pass a callback to your build function so you can be notified immediatly when your field is set.

Also placing var self = this inside your method ensures that you can call object.build.call(scope, f) without it being bound to the wrong scope.

This pattern is particularly useful in inheritance when you want to call a method of another class.

In the future, try using the Module Pattern:

HERE IS THE MODULE PATTERN:

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

HERE IS WHAT YOUR OBJECT MIGHT LOOK LIKE:

var myNamespace = (function($) 
{
    var publicInstances = {};

    // ***********************
    // myObject
    publicInstances.myObject = myObject;
    function myObject(url, data) {

        /// <summary>A pointer to this</summary>
        var self = this;
        /// <summary>Contains the result</summary>
        this.field = null; // Name this result instead.

        this.url = url;
        this.data = data;

        this.initialize = function() {
            /// Initialize whatever
        }
        this.build = function() {
            $.get(self.url, self.data, function(result) {
                self.field = result;
                cb(self.field);
            });
        }
        this.cb = function(result) {
            /// Do whatever
        }

        self.initialize();
    }

    return publicInstances;
})(jQuery);