Javascript 如何从$.post()调用中引用对象

Javascript 如何从$.post()调用中引用对象,javascript,jquery,javascript-objects,Javascript,Jquery,Javascript Objects,我有一个Javascript对象,看起来像: var MyObject = { func1 : function() { // Does something }, func2 : function() { // Send an AJAX request out $.post('', $('form').serialize(), function(response) { // Call the first funct

我有一个Javascript对象,看起来像:

var MyObject = {
   func1 : function() {
       // Does something
   },
   func2 : function() {
       // Send an AJAX request out
       $.post('', $('form').serialize(), function(response) {
           // Call the first function
           this.func1(); // Fails - this refers to the $.post request
       }, 'json');
   }
};
如何使
引用点指向对象本身,而不是
$。post
请求

如何从$.post()调用中引用对象

使用变量名:

var MyObject = {
   func1 : function() {
       // Does something
   },
   func2 : function() {
       // Send an AJAX request out
       $.post('', $('form').serialize(), function(response) {
           // Call the first function
           MyObject.func1(); // <== Using the name
       }, 'json');
   }
};
或ES5的:

var MyObject={
func1:函数(){
//做点什么
},
func2:函数(){
//发送一个AJAX请求
$.post(“”,$('form').serialize(),函数(响应){
//调用第一个函数
this.func1();//
如何从$.post()调用中引用对象

使用变量名:

var MyObject = {
   func1 : function() {
       // Does something
   },
   func2 : function() {
       // Send an AJAX request out
       $.post('', $('form').serialize(), function(response) {
           // Call the first function
           MyObject.func1(); // <== Using the name
       }, 'json');
   }
};
或ES5的:

var MyObject={
func1:函数(){
//做点什么
},
func2:函数(){
//发送一个AJAX请求
$.post(“”,$('form').serialize(),函数(响应){
//调用第一个函数
这个.func1();//是最简单的方法:

var MyObject = {
   func1 : function() {
       // Does something
   },
   func2 : function() {
       var self = this;        //self = MyObject-object
       // Send an AJAX request out
       $.post('', $('form').serialize(), function(response) {
           // Call the first function
           self.func1(); // #### NEW ####
       }, 'json');
   }
};
最简单的方法:

var MyObject = {
   func1 : function() {
       // Does something
   },
   func2 : function() {
       var self = this;        //self = MyObject-object
       // Send an AJAX request out
       $.post('', $('form').serialize(), function(response) {
           // Call the first function
           self.func1(); // #### NEW ####
       }, 'json');
   }
};

使用ajax上下文选项:

$.ajax({
  context:this,
  type: "POST",
  url: url,
  data: data,
  success: this.func1,
  dataType: dataType
});

使用ajax上下文选项:

$.ajax({
  context:this,
  type: "POST",
  url: url,
  data: data,
  success: this.func1,
  dataType: dataType
});

如果根本不使用它:
this.func1();
=>
func1();
@雪盲:不,这将导致
引用错误。
func1
在上述
func2
范围内没有任何
func1
。@T.J.Crowder哦,你说得对。
this.func1();
func1()
@SnowBlind:No,那将导致
引用错误
。上面的
func2
范围内没有
func1
。@T.J.Crowder哦,你是对的。
这实际上为什么不
引用给
$的回调函数。post
而不是
$。post
本身?因为当你声明使用
var Foo function(){}的对象类型使用函数中的
这个
指函数本身。编辑,我要问一个问题。你太棒了,T.J.!正是我想要的答案for@Virus721:
很少涉及函数。至于
$.post
,文档没有说明在回调期间将此
设置为什么@FloatingRock:LOL!很高兴这有帮助。:-)实际上为什么
这个
不引用
$.post
而不是
$.post
本身的回调函数呢?因为当您使用
var Foo function(){}声明对象类型时使用函数中的
这个
指函数本身。编辑,我要问一个问题。你太棒了,T.J.!正是我想要的答案for@Virus721:
很少涉及函数。至于
$.post
,文档没有说明在回调期间将此
设置为什么@FloatingRock:LOL!很高兴这有帮助。:-+1不确定我为什么不包括该选项,这是我通常会做的事情(具体到变量的名称!)。有时少就是多:-)
var that=this;
+1不确定我为什么不包括该选项,这是我通常会做的事情(具体到变量的名称!)。有时少就是多:-)
var that=this;