あるコードをみていて、
javascriptのクラスの定義時に
プロトタイプのメソッドの中に
return this;としているのは、なぜでしょうか?
また、prototype をローカル変数にするメリットは
なんでしょうか?
勉強不足で申し訳ありませんが、よろしくお願いいたします。
javascript
1var index = index || {}; 2 3(function($) { 4 index.MyClass = (function() { 5 function MyClass() { 6 this.props = { 7 prop1: prop1, 8 prop2: prop2 9 }; 10 this._init(); 11 } 12 13 // prototype をローカル変数へ 14 var p = MyClass.prototype; 15 16 p._init = function() { 17 var self = this; 18 $window.scroll(function() { 19 //省略 20 }); 21 }; 22 23 p._controller = function(y) { 24 if (this.props.prop1 < y) { 25 this.methodA(); 26 } else { 27 this.methodB(); 28 } 29 }; 30 31 p.methodA = function() { 32 33 /* 34 省略 35 */ 36 37 return this; 38 }; 39 40 p.methodB = function() { 41 42 /* 43 省略 44 */ 45 46 return this; 47 }; 48 49 // class MyClass 50 return MyClass; 51 }()); 52 53 $(function() { 54 index.myclass = new index.MyClass(); 55 56 }); 57 58}(jQuery)); 59
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/06/16 02:46