下記のソースは、初歩的なモーダルウィンドウが記述されたものです。
ソースの流れとして
function Modal(el) { this.initialize(el); } Modal.prototype.initialize = function(el) {//initialize…初期化する this.$el = el; this.$container = $("#modal"); this.$contents = $("#modal-contents"); this.$close = $("#modal-close"); this.$next = $("#modal-next"); this.$prev = $("#modal-prev"); this.$overlay = $("#modal-overlay"); this.$parents = this.$el.parents("ul"); this.$window = $(window); this.handleEvents(); }; Modal.prototype.handleEvents = function() { var self = this; this.$parents.on("click","a", function(e) { self.show(e); return false; }); this.$close.on("click",function(e) { self.hide(e); return false; }); this.$overlay.on("click",function(e) { self.hide(e); return false; }); this.$next.on("click", function(e) { self.next(e); return false; }); this.$prev.on("click", function(e) { self.prev(e); return false; }); this.$window.on("load resize", function() { self.resize(); }); }; Modal.prototype.show = function(e) { var $target = $(e.currentTarget), src=$target.attr("href"); this.$contents.html("<img src=\"" + src +"\"/>"); this.$container.fadeIn(); this.$overlay.fadeIn(); var index = $target.data("index"); this.countChange = this.createCounter(index,this.$el.length); return false; }; Modal.prototype.hide = function(e) { this.$container.fadeOut(); this.$overlay.fadeOut(); }; Modal.prototype.slide = function(index) { this.$contents.find("img").fadeOut({ complete: function() { var src = $("[data-index=\"" + index + "\"]").find("img").attr("src"); $(this).attr("src",src).fadeIn(); } }); }; Modal.prototype.createCounter = function(index, len) { return function(num) { return index = (index + num + len) % len; }; }; Modal.prototype.next = function() { this.slide(this.countChange( 1 )); }; Modal.prototype.prev = function() { this.slide(this.countChange( -1 )); }; Modal.prototype.resize = function() { var w = this.$window.width(); if(w<640) { this.$container.css({"width":"320","height":"213"}); } else { this.$container.css({"width":"750","height":"500"}); } }; $("#more-btn").on("click", function() { var html = "<li><a href=\"photo-04.jpg\" data-index=\"3\"><img alt=\"\" src=\"photo-04.jpg\" width=\"160\" class=\"img-thumbnail\"></a></li>"; $(html).appendTo($("#modal-thumb")).hide().fadeIn(); $(this).fadeOut(); modal.$el = $("#modal-thumb a"); }); var modal = new Modal($("#modal-thumb a"));
Modalが実体化されるとプロトタイプオブジェクトプロパティinitializeのメソッドinitialize()が実行され、さらにinitialize()内でプロパティの初期化をおえるとhandleEventsが実行されます。
しかし、イベントが発生しないと何も始まらないので待機します。そしてModalを実体化したときに渡した実引数$("#modal-thumb a")や、initialize内で初期化したjqueryオブジェクトに渡したセレクタなどがクリックされたら、同じくModalのプロトタイプであるshowメソッドやhideメソッドなどにイベントオブジェクトを実引数として渡したうえで実行する、という流れになっております。
ここで質問です。このソース最下部で実体化したコンストラクタModalからinitializeを実行しさらにinitialize()内でプロパティの初期化をおえるとhandleEventsが実行されますが、イベントが発生しないと何も始まらないので待機します。
そしてModalを実体化したときに渡した実引数$("#modal-thumb a")や、initialize内で初期化したjqueryオブジェクトに渡したセレクタなどがクリックされたら、同じくModalのプロトタイプであるshowメソッドやhideメソッドなどにイベントオブジェクトを実引数として渡したうえで実行する、という流れですが、このソースの流れはjavascriptにおける公理(一種の決まりのようなもの)なのでしょうか。
つまりvarのように絶対付けないといけないわけではないが、付けないと後々に不具合のもととなるので任意で付けているように何かしらソースを構築する上でここにあれを、それをあそこにというような決まった、或いはそうしたほうがいいよね、というソースの流れがあるのでしょうか。このソースもそのような流れの公理があってそのようにしているのでしょうか。
回答2件
あなたの回答
tips
プレビュー