3

我有一个简单的 jQueryready事件,它通过调用对象中的 a 函数来初始化视图setupView

我的问题是,setSomethingImportantinit函数调用函数的适当方法是什么,如下所示?

init由于调用是从与函数不同的执行上下文进行的,this.setSomethingImportant()因此不起作用。但是,如果我使用setupView.setSomethingImportant(). 我遇到的问题是,如果 var name ( setupView) 发生更改,我也必须更改代码主体。

   (function() {        
        $(document).ready(function() {              
            setupView.init();               
        });     
         var setupView = {          
            currentState : "CT",            
            init : function () {
               $("#externalProtocol").change( function () {
                    console.log("Changed =" + $(this).val());
                    setSomethingImportant(); 
                               // Question ? how to call a method in the setupView object   
                });         
             },         
             setSomethingImportant : function () {
                 this.currentState="TC";    
                 console.log("Something has changed :" + this.currentState );
             }      
         }  
 }(jQuery);
4

3 回答 3

3

存储this到变量中:

var setupView = {
    currentState: "CT",
    init: function() {
        // Keep a reference to 'this'
        var self = this;
        $("#externalProtocol").change(function() {
            console.log("Changed =" + $(this).val());

            // Use the old 'this'
            self.setSomethingImportant();
        });
    },
    setSomethingImportant: function() {
        this.currentState = "TC";
        console.log("Something has changed :" + this.currentState);
    }
};

请参阅工作演示

于 2012-08-09T16:09:31.980 回答
1

只需单独声明该函数,然后像这样调用:

function setSomethingImportant(context) {
    context.currentState="TC";    
    console.log("Something has changed :" + context.currentState );
};

(function() {        
        $(document).ready(function() {              
            setupView.init();               
        });     
         var setupView = {          
            currentState : "CT",            
            init : function () {
               $("#externalProtocol").change( function () {
                    console.log("Changed =" + $(this).val());
                    setSomethingImportant(this); 
                               // Question ? how to call a method in the setupView object   
                });         
             },         
             setSomethingImportant : function () {
                 setSomethingImportant(this);
             }      
         }  
}(jQuery);
于 2012-08-09T16:09:30.560 回答
1

Please note that I changed my original solution. I am now passing data to the event handler using even.data.

(function() {        
    $(document).ready(function() {              
        setupView.init();               
    });     
    var setupView = {          
        currentState : "CT",            
        init : function () {
            $("#externalProtocol").change({ _this: this }, function (event) {
                console.log("Changed =" + $(this).val());
                event.data._this.setSomethingImportant(); 
            });         
        },         
        setSomethingImportant : function () {
            this.currentState="TC";    
            console.log("Something has changed :" + this.currentState );
        }      
    }  
 }(jQuery);
于 2012-08-09T16:11:34.797 回答