Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:14:21+00:00 2026-05-31T22:14:21+00:00

As far as I understand, visitor pattern is often used to add methods to

  • 0

As far as I understand, visitor pattern is often used to add methods to some hierarchy structure. But I still don’t get it: see the example where I try to highlight left subtree:

subtree highlighting

Javascript tree implementation:

  function node(val) {
    this.value = val;
    this.left = this.right = null;
  }

  var tree = new node("A");
  tree.left = new node("B1");
  tree.right = new node("B2");
  tree.left.left = new node("C1");
  tree.left.right = new node("C2");

I think I am using visitor pattern highlighting:

node.prototype.accept = function(visitorObj) {
  visitorObj.visit(this);
}

function visitor() {
  var that = this;
  this.visit = function(tgt) {
    tgt.value = "*"+tgt.value;
  }
  this.highlight = function(tgt) {
    tgt.accept(that);
    if(tgt.left) that.highlight(tgt.left);
    if(tgt.right) that.highlight(tgt.right);
  }
}

(new visitor()).highlight(tree.left);

But why to use accept-visit methods, when it can be more straightforward?

function visitor() {
  var that = this;
  this.highlight = function(tgt) {
    tgt.value = "*"+tgt.value;
    if(tgt.left) that.highlight(tgt.left);
    if(tgt.right) that.highlight(tgt.right);
  }
}

(new visitor()).highlight(tree.left);

It is similar to this example. Does it mean that if language mix types (like javascript), there is no reason for accept-visit pair at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:14:23+00:00Added an answer on May 31, 2026 at 10:14 pm

    You missed something in the implementation. Imagine that the left and the right properties of the Node element were private. Then how would you highlight them in your implementation?

    The visitor should not be aware of the tree structure and let the node element run the visitor on any sub-element which is a direct child. So your code should look like this:

    node.prototype.accept = function(visitorObj) {
        visitorObj.visit(this);
        if (this.left) this.left.accept(visitorObj);
        if (this.right) this.right.accept(visitorObj);
    }
    
    function visitor() {
        var that = this;
        this.visit = function(tgt) {
            tgt.value = "*"+tgt.value;
        }
        this.highlight = function(tgt) {
            tgt.accept(that);
        }
    }
    
    (new visitor()).highlight(tree.left);
    

    This way the visitor has no knowledge of the structure of the tree, it is generic and will work on any node that has the property “value”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I can understand HTML.ActionLink and URL.RouteURL but still trying to absorb where
As far as I understand, if I want to get my database under source
As far as I understand the branch-on-sign is the name of some kind of
As far as I understand, every string is an object in Javascript. Still, it
As far as i understand ViewModel communicates with View via databinding. But how can
As far as I understand, the purpose of a Bridge pattern is, quoting from
As far as I understand, binding to a var in ActionScript is carried out
As far as I understand, https is http plus SSL/TLS. What do I need
As far as I understand, the onchange event of a text input element is
As far as I understand the serial port so far, transferring data is done

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.