简体   繁体   English

调用obj函数不是正确的方法吗?

[英]Is it not a right way to call my obj function?

I have a function which is return a object. 我有一个返回对象的函数。 in the object i have two function to show the popup and close it. 在对象中,我有两个功能来显示弹出窗口并关闭它。 it works within the parent function, but it's not from out side.. is it not a right way to call that.. else how can i call the obj function from out side? 它在父函数内工作,但不是从外部开始..这不是正确的调用方法..否则我如何从外部调用obj函数?

my function : 我的功能:

var popupHandler = function(handler,msg,popUp){

    msg =  msg == "help" ? "help" : "results"
    $(handler).click(function(){
        popObj.showPop(); //works
    })
    $('.cls-how2play').click(function(){
        if(msg == 'help') popObj.closePop(); //works
    });

    var popObj = {
        showPop  : function(){
                    if(!(popUp).is(':visible')) $(popUp).fadeIn().children().find('.'+msg).show().siblings().hide();
                },
        closePop : function(){
                    $(popUp).fadeOut()
                }
    }
    return popObj;
}

from calling ouside like this : 从这样调用ouside:

 $('.ui-footer').click( function(){ 
  var closeIt = popupHandler(); 
  closeIt.popObj.closePop() }) //not works.. why?
}

any one can help me the right way to call the obj functions from outside of the returning function? 有谁能帮助我从返回函数外部调用obj函数的正确方法?

thanks. 谢谢。

Rather than 而不是

closeIt.popObj.closePop()

You want 你要

closeIt.closePop()

Your popupHandler function returns the popObj object, which has the showPop and closePop functions on it. 您的popupHandler函数返回 popObj对象,该对象上具有showPopclosePop函数。 So closeIt is a reference to that same object. 因此closeIt是对同一对象的引用。

As you are returning the popObj , your closeId will get only the two functions, not wrapped in the popObj object. 当您返回popObjcloseId将仅获得两个函数,而不包装在popObj对象中。 Therefor you will call the function like so, without popObj : 因此,您将像这样调用该函数,而无需popObj

closeIt.closePop();

您应该致电:

closeIt.closePop();

There is no need to wrap this in an object since you immediately return it. 由于您立即将其返回,因此无需将其包装在一个对象中。

you can write 你可以写

return{
showPop  : function(){
                    if(!(popUp).is(':visible')) $(popUp).fadeIn().children().find('.'+msg).show().siblings().hide();
                },
        closePop : function(){
                    $(popUp).fadeOut()
                }
}

Now closeIt.closePop(); 现在closeIt.closePop(); should work very well. 应该工作得很好。

As i can c 尽我所能

popupHandler is a function, and popObj is a return result of the function "popupHandler" popupHandler是一个函数,而popObj是函数“ popupHandler”的返回结果

when the program run to 当程序运行到

var closeIt = popupHandler();

it means that the "closeIt" assigned by the result of the function "popupHandler", a obj as the same as the "popObj". 它表示由函数“ popupHandler”的结果分配的“ closeIt”,它的对象与“ popObj”相同。

you can consider that "closeIt" is a copy of "popObj". 您可以认为“ closeIt”是“ popObj”的副本。

and "popObj" is not a property of "closeIt" , them are the same. 和“ popObj”不是“ closeIt”的属性,它们是相同的。

so you should code closeIt.closePop(), but not closeIt.popObj.closePop(), 因此您应该编写closeIt.closePop()的代码,而不是closeIt.popObj.closePop()的代码,

and not popObj.closePop() as well. 而不是popObj.closePop()。

because popObj was "var"ed in the declare of the popupHandler, it belonged that scope. 因为popObj在popupHandler的声明中是“变量”,所以它属于该范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM