I'm really not sure if this is possible in Javascript. Here's my function:
var tree = function(name, callback) {
if (this.name) {
this.name.push(name)
print(this.name)
} else {
this.name = []
}
callback()
}
I'd like to use it as follows and print out the heirarchy:
tree("john", function() {
tree("geoff", function() {
tree("peter", function() {
tree("richard", function() {
})
})
})
tree("dave", function() {
})
})
Here's the desired output:
// ['john']
// ['john', 'geoff']
// ['john', 'geoff', 'peter']
// ['john', 'geoff', 'peter', 'richard']
// ['john', 'dave']
but unfortunately I'm getting
// ['john', 'geoff', 'peter', 'richard', 'dave']
for the last function call. Is there a way to get the desired outcome?
Kind regards
Adam Groves