2011-08-04 2 views
0

イム今、PHPの男、

$.plugin.method({ 
    test: 'helloworld' 
}); 

$.plugin.method({ 
    test: 'another helloworld' 
}) 

件まで何イムHERESに私の関数やクラスHERESに私の最初のプラグイン

を作る上でイム学習?

test = ['helloworld','another helloworld'] 

を期待して何イム

// class ? 
jquery.plugin = function(){ 
    // variables 
    var test = []; 

    // function ? 
    var method = function(params){ 
     test[] = params['test'] 
    } 
    console.log(test) 
} 

は、我々はJavaScriptでそれを行うことができますか?私はそれを正しくしていますか?

ありがとうございました!

+0

試しましたか? – JJJ

+0

ええ、私はその空をした。もう一度お試しください。ああ、申し訳ありませんが間違った方法。質問が更新されました。ごめんなさい。 –

+0

フェリックスなので、$ .plugin.method()はそれ自身の機能ですか?プラグインの中でメソッドを呼び出さない? –

答えて

3

を。

あなたはmethodプロパティでpluginオブジェクトを作る必要があるだろう:

(function($) { 

    // variables 
    var test = []; 

    $.plugin = { 
     method: function(params){ 
      test.push(params['test']); 
      console.log(test) 
     } 
    } 

}(jQuery)); 

即時関数はtest自体$.pluginにのみ表示されていることを保証します。あなたは外からそれにアクセスすることはできません。あなたがいることをしたい場合、あなたはそれ$.pluginの財産行う必要があります。

$.plugin = { 
    test: [], 
    method: function(params){ 
     this.test.push(params['test']); 
     console.log(test) 
    } 
} 

私はあなたが最初functions [MDN guide]objects [MDN guide]についての基本を学ぶためにJavaScript guide [MDN guide]を読むことをお勧め。

+0

素晴らしいリンク!!!!おかあさんfelix –

+0

@アダム:歓迎です:) –

1

$ .pluginとは何ですか?私はここに、あなたは何をしたい見当がつかない何も行かない:あなたの例では、あなたがplugin機能をしたが、最初のスニペットで、あなたが$.plugin.method()なく$.plugin()を呼び出している

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> 
<script type="text/javascript"> 
$.plugin = { 
    // Variable 
    test: [], 

    // Function 
    method: function(params) { 
     this.test.push(params['test']); 
    } 
}; 

$.plugin.method({ 
    test: 'helloworld' 
}); 

$.plugin.method({ 
    test: 'another helloworld' 
}); 

alert($.plugin.test); 
</script> 
+0

ありがとうございます。わかった。 –