Skip to content

Instantly share code, notes, and snippets.

@nanasess
Last active March 27, 2018 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanasess/53cb08a0ce297eae0416aa154a1c32aa to your computer and use it in GitHub Desktop.
Save nanasess/53cb08a0ce297eae0416aa154a1c32aa to your computer and use it in GitHub Desktop.
ツリーを処理するサンプル
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