0

内部関数のshowまたはhideを呼び出すことができないため、何が間違っているのかを知る必要がありますか?

(function()
{
    var Fresh = {
        notify:function()
        {
            var timeout = 20000;
            $("#notify-container div").get(0).id.substr(7,1) == "1" && (show(),setTimeout(hide(),timeout));
            var show = function ()
            {
                $("body").animate({marginTop: "2.5em"}, "fast", "linear");
                $("#notify-container div:eq(0)").fadeIn("slow");
            },
            hide = function()
            {
               $("#notify-container div").hide();
            }
        }//END notify
    }
    window.Fresh = Fresh;
})();
Fresh.notify();

ありがとう、リチャード

4

5 に答える 5

3

アップデート

次のようなことができるようにしたい場合は、次のようなことを行うFresh.notify.showMessage()必要があります。関数にプロパティを割り当てるだけですnotify

var Fresh = {notify:function(){return 'notify called';}};
Fresh.notify.showMessage = function () { return this() + ' and showMessage, too!';};
Fresh.notify();//notify called
Fresh.notify.showMessage();//notify called and showMessage, too!

これはここで関数オブジェクトを指し、そのように呼び出すことができます(this()=== Fresh.notify();)。それだけです。

このコードには多くの問題があります。まず第一に、クロージャを使おうとしているのは素晴らしいことです。しかし、私の言うことを気にしないのであれば、あなたはそれらを最大限に活用していません。例:notifyメソッドには、関数宣言とjQueryセレクターが含まれています。これは、メソッドが呼び出されるたびに、新しい関数オブジェクトが作成され、セレクターによってdomが何度も検索されることを意味します。クロージャスコープで参照されている関数とdom要素だけを保持することをお勧めします。

(function()
{
    var body = $("body");
    var notifyDiv = $("#notify-container div")[0];
    var notifyDivEq0 = $("#notify-container div:eq(0)");
    var show = function ()
    {
        body.animate({marginTop: "2.5em"}, "fast", "linear");
        notifyDivEq0.fadeIn("slow");
    };
    var hide = function()
    {//notifyDiv is not a jQ object, just pass it to jQ again:
        $(notifyDiv).hide();
    };
    var timeout = 20000;
    var Fresh = {
        notify:function()
        {
            //this doesn't really make sense to me...
            //notifyDiv.id.substr(7,1) == "1" && (show(),setTimeout(hide,timeout));
            //I think this is what you want:
            if (notifyDiv.id.charAt(6) === '1')
            {
                show();
                setTimeout(hide,timeout);//pass function reference
                //setTimeout(hide(),timeout); calls return value of hide, which is undefined here
            }
        }//END notify
    }
    window.Fresh = Fresh;
})();
Fresh.notify();

この場合、提案を行うのは困難ですが、それ自体では、このコードはあまり意味がありません。コードが機能していることを確認できるように(またはコードが失敗することを確認できるように)フィドルを設定することをお勧めします:P)

于 2012-09-14T09:36:13.777 に答える
2

showまず、まだ定義されていないときに値を使用しようとしています(ただしshow、そのスコープには変数が存在します)。

function test() {
  show(); // TypeError: show is not a function
  var show = function() { console.log(42); };
}

呼び出されるポイントより上に線を移動することで簡単に修正できvar showます。

function test() {
  var show = function() { console.log(42); };
  show();
}
test(); // 42

function show() { ... }...または、関数をより「従来の」方法で(表記法で)定義する場合。

function test() {
  show();
  function show() { console.log(42); };
}
test(); // 42

次に、代わりにこれを使用する必要があります。

... && (show(), setTimeout(hide, timeout) );

...これは関数名であり、関数の結果ではないためsetTimeout、最初の引数として渡す必要があります。

于 2012-09-14T09:19:32.043 に答える
2

前にshowとhideを定義する必要があります。また、彼らが言ったようにhide()を変更する必要があります。結果は次のようになります。

(function()
{
    var Fresh = {
        notify:function()
        {
            var show = function()
            {
                $("body").animate({marginTop: "2.5em"}, "fast", "linear");
                $("#notify-container div:eq(0)").fadeIn("slow");
            },
            hide = function()
            {
               $("#notify-container div").hide();
            },
            timeout = 20000;
            $("#notify-container div").get(0).id.substr(7,1) == "1" && ( show(), setTimeout(hide,timeout) );

        }//END notify
    }
    window.Fresh = Fresh;
})();
Fresh.notify();
于 2012-09-14T09:36:13.627 に答える
1

私はショーを呼び出す順序、非表示が問題だと思います。コードを変更しました。それはうまくいきます。リンク http://jsfiddle.net/dzZe3/1/にアクセスしてください

于 2012-09-14T09:35:37.927 に答える
0

(show()、setTimeout(hide()、timeout));

少なくともする必要があります

(show(),setTimeout(function() {hide()},timeout)); 

また

(show(),setTimeout(hide,timeout));    
于 2012-09-14T09:21:43.057 に答える