Commits

Keiji Matsuzaki  committed 22d210b

function was converted to jquery style.

  • Participants
  • Parent commits 3fabff5

Comments (0)

Files changed (1)

File question_3/main.js

+(function(window){
+  var MyApp = {
+    init: function(){
+      var lists = $.traverse(document.getElementById("target"));
+      console.log(lists);
+    },
+    slice: function(list, start, stop){
+      var _r = [];
+      if(stop == undefined){
+        stop = list.length;
+      }
+      for(var i = start; i < stop; i++){
+        if(list[i] != undefined){
+          _r.push(list[i]);
+        }
+      }
+      return _r;
+    },
+    traverse: function(x){
+      if(x.length > 1){
+        var _s = $.slice(x, 1);
+        if(_s.length > 0){
+          return [$.traverse(x[0])].concat($.traverse(_s));
+        }
+      }
+      else if(x.length == 1){
+        return $.traverse(x[0]);
+      }
+      if(x.childNodes != undefined && x.hasChildNodes()){
+        var childlen = x.childNodes;
+        return [x].concat($.traverse(childlen));
+      }
+      else{
+        return x;
+      }
+    }
+  };
+
+  window.MyApp = window.$ = MyApp;
+})(window);
+
 if(document.addEventListener){
-  document.addEventListener("DOMContentLoaded", exec, false);
+  document.addEventListener("DOMContentLoaded", $.init, false);
 }
 
-function exec(){
-  console.log("start.");
-  var target = document.getElementById("target")
-    , ar = [];
-  console.log(traverce(target));
-};
-
-function slice(list, start, stop){
-  var _r = [];
-  if(stop == undefined){
-    stop = list.length;
-  }
-  for(var i = start; i < stop; i++){
-    if(list[i] != undefined){
-      _r.push(list[i]);
-    }
-  }
-  return _r;
-};
-
-function traverce(x){
-  if(x.length > 1){
-    var _s = slice(x, 1);
-    if(_s.length > 0){
-      return [traverce(x[0])].concat(traverce(_s));
-    }
-  }
-  else if(x.length == 1){
-    return traverce(x[0]);
-  }
-  if(x.childNodes != undefined && x.hasChildNodes()){
-    var childlen = x.childNodes;
-    return [x].concat(traverce(childlen));
-  }
-  else{
-    return x;
-  }
-}