Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图通过嵌套函数在对象中存储值_Javascript_Object_Scope_Storage - Fatal编程技术网

Javascript 试图通过嵌套函数在对象中存储值

Javascript 试图通过嵌套函数在对象中存储值,javascript,object,scope,storage,Javascript,Object,Scope,Storage,我一直在努力遵循最佳实践,并在一个对象中包含我的函数等 目标是尝试在调用时运行两个方法,一个用于初始化并在单击时获取起始值。。。然后使用另一种方法,该方法在用户单击“计算”按钮时运行,以基于初始起始值执行简单计算 问题是,即使我将这些方法包装在匿名函数中,据我所知,它们也应该引用父对象属性的值。在init中,它正确地设置了它,并对值进行了输出,这表明它获取了ex的正确值77 然后我希望现在仍然在同一页上,myObj.beginVal应该有一个一致的值77,直到更改为止 但是,在运行“calcul

我一直在努力遵循最佳实践,并在一个对象中包含我的函数等

目标是尝试在调用时运行两个方法,一个用于初始化并在单击时获取起始值。。。然后使用另一种方法,该方法在用户单击“计算”按钮时运行,以基于初始起始值执行简单计算

问题是,即使我将这些方法包装在匿名函数中,据我所知,它们也应该引用父对象属性的值。在init中,它正确地设置了它,并对值进行了输出,这表明它获取了ex的正确值77

然后我希望现在仍然在同一页上,
myObj.beginVal
应该有一个一致的值77,直到更改为止

但是,在运行“calculate”时,它没有该值,而是初始空值。 我希望它的值是init函数设置的值,在本例中是77

没有页面被重新加载,我已经尝试将
myObj
对象的创建放在文档内外,并准备好相同的结果。。。我还希望,当其他方法试图访问它时,它将保持其价值

我如何做到这一点?我做错了什么

任何帮助都将不胜感激

myObj = {};

$(document).ready(function(){

    myObj = {               
        beginVal : null,  //initial value...

        init : function(evt) {

            $("#openCalculatorButton").bind("click", function() {

            myObj.beginVal = $($(this).parent().attr('id')); // 77

            $.fancybox.showActivity();

            $.ajax({
                type    : "POST",
                cache   : false,
                url     : "/calc.php",
                data    : myObj,
                success : function(data) {
                    $.fancybox({
                        'content' : data
                    });
                }
            });

            return false;
            });
        }, // function(evt)                             


        calculate : function(evt) {

            console.log(myObj.beginVal); // null

            $('#getAnswerButton').click(function(){ 
                // Need the value of myObj.beginVal to be retained and at this point be 77! but it is NULL!         

                myObj.answer = myObj.beginVal * 3.16;  // Create prop and set value of the original object

                return false;
            });

        }

    }// calc
});

您应该绑定单击处理程序,以便与文档中的对象进行交互

myObj = {               
        beginVal : null,  //initial value...
        answer : null
        init : function(id){

            this.beginVal = id;

            $.fancybox.showActivity();

            $.ajax({
                type    : "POST",
                cache   : false,
                url     : "/calc.php",
                data    : myObj,
                success : function(data) {
                    $.fancybox({
                        'content' : data
                    });
                }
            });
        }
        calc : function(){

            if (!!this.beginVal){ //check to see if beginVal is not null
                tis.answer = this.beginVal * 3.16;  // Create prop and set value of the original object
            }
        }

}

$(document).ready(function(){

        $("#openCalculatorButton").bind("click", function() {

            myObj.init( $($(this).parent().attr('id')) ); // 77

        });

        $('#getAnswerButton').click(function(){

            myObj.calc();

        });

});

init
calc
函数对我来说运行良好

myObj = {};
$(document).ready(function(){
    myObj = {               
        beginVal : null,  //initial value...
        init : function(evt) {
            myObj.beginVal = 77; // 77
            return false;
        }, // function(evt)                             
        calculate : function(evt) {
            alert(myObj.beginVal);
            myObj.answer = myObj.beginVal * 3.16;  // Create prop and set value of the original object
            return false;
        }
    }

    myObj.init();
    myObj.calculate();
});
您的问题似乎在于ajax调用、绑定函数或单击函数


此方法允许您重用代码,如果需要,可以实例化多个计算器

$(document).ready(function(){

    var calculator = function(initVal, calcButtonID, ansButtonID) {               
            val : initVal,  //initial value...
            calcID: calcButtonId,
            ansID: ansButtonID,
            answer: null,
            init : function() {
                       $("#" + calcID).bind("click", function() {
                           $.fancybox.showActivity();

                           $.ajax({
                               type    : "POST",
                               cache   : false,
                                url     : "/calc.php",
                                data    : this.val,
                                success : function(data) {
                                    $.fancybox({
                                        'content' : data
                                    });
                                }
                           });

                      });

                      $('#' + ansID).bind('click', this.calculate());
            },                        


            calculate : function() { 
                this.answer = this.val * 3.16;
                return this.answer;

            }

       }

    var myCalc = new calculator($($(this).parent().attr('id')),
                            'openCalculatorButton',
                            'getAnswerButton');

    //Usage
    var someAnswer = myCalc.calculate();
});

谢谢我单独测试了ajax返回的数据,但是单击处理程序等也可以正常工作。我的问题是click evt中的函数显然没有得到更新的值。document.ready()是否在每次事件发生时重新运行并声明对象?这可能是我的问题吗?