Skip to content

Conversation

@revan730
Copy link
Member

@revan730 revan730 commented Mar 4, 2017

Implemented tree using ES6 classes, added next methods:

  • findFirst(name) : Node
  • find(name, callback)
  • findAll(name) : [Node]
  • setParent(newParent)

Copy link
Member

@tshemsedinov tshemsedinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В общем код достаточно внятный и чистый

}

findAll(name) { // Find all nodes with specified name
let arr = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

find(name, callback) { //Call function for each found node
const nodes = this.findAll(name);
if (nodes.length > 0)
for (let n of this.findAll(name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

со скобками {} будет лучше и if и for

}

setParent(newParent) { // Sets new parent for this node
this.parent.childs.pop(this.parent.childs.indexOf(this));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это таб?


console.log('findAll("searched") -', root.findAll('searched'));
console.log('findFirst("searched") -', root.findFirst('searched'));
root.find('n2', n => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот тут {} можно опустить
n => console.log(...

findAll(name) { // Find all nodes with specified name
let arr = [];
if (this.name === name) arr.push(this);
for (let n of this.childs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут ходить по массиву обычным циклом, for..of пока медленный


findFirst(name) { //Find first instance
if (this.name === name) return this;
for (let n of this.childs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже без for..of лучше избавиться, а переменную содавать до цикла:

let i;
for (i = 0; ...

это работает быстрее

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants