Last active
March 27, 2018 00:16
-
-
Save nanasess/53cb08a0ce297eae0416aa154a1c32aa to your computer and use it in GitHub Desktop.
ツリーを処理するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Category = function (name, child) { | |
var self = this; | |
this.name = name; | |
this.children = child; | |
this.accept = function(Visitor) { | |
Visitor.visit(self); | |
}; | |
}; | |
var Visitor = function () { | |
this.visit = function(Category) { | |
console.log(Category.name); | |
var len = Category.children.length; | |
if (len > 0) { | |
for (var i = 0; i < len; i++) { | |
Category.children[i].accept(this); | |
} | |
} | |
} | |
}; | |
var Categories = [new Category('aaa', []), new Category('bbb', [new Category('ccc', [new Category('ddd', [])])])]; | |
for (var i = 0; i < Categories.length; i++) { | |
Categories[i].accept(new Visitor()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment