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

  • Home
  • SEARCH
  • 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 3974528
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:34:42+00:00 2026-05-20T04:34:42+00:00

I want, in javascript, to implement the template method pattern. I have a PropertyDecorator

  • 0

I want, in javascript, to implement the template method pattern.

I have a PropertyDecorator with some subclasses: OpenButtonDecorator, SeeButtonDecorator and so on. I want to have in Property decorator the next function:

var build = function(){
   decorate(); //Abstract in PropertyDecorator, defined in subclasses
   return le.build();
}

How can I get this scenario working? Maybe I implemented wrong the inheritance :S (help with that too 🙂 )

Thank you in advance.

  • 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-20T04:34:43+00:00Added an answer on May 20, 2026 at 4:34 am

    Javascript is a dynamic typed, prototype-based language. Template method is a design pattern and hence language independent, but its implementation can vary across languages.

    In the case of javascript, and also in other dynamically typed languages, like ruby, abstract classes and interfaces doesn’t make much sense, since dynamic linking occurs via delegation. (A method call is propagated to higher levels in the inheritance tree until a prototype can handle the request). This, in conjunction with duck-typing, which means that any method can be potentially called on any instance, avoids the need of an explicit contract, which in class-based languages is defined by those declared methods that are visible on a certain Type.

    So in order to implement the pattern, just call an inexistent method on the parent’s prototype build method (that method will be the template) and simply implement that method on the sublcasses:

    function PropertyDecorator()
    {
       this.build = function()
       {
          var decoration=this.decorate();
          return "The decoration I did: "+decoration;
       };
    }
    
    //we set the parent (those prototype that instances of this class will delegate calls to) 
    OpenButtonDecorator.prototype = new PropertyDecorator();
    function OpenButtonDecorator()
    {
       this.decorate = function()
       {
         return "open button";
       };
    }
    
    
    SeeButtonDecorator.prototype = new PropertyDecorator();
    function SeeButtonDecorator()
    {
       this.decorate = function()
       {
          return "see button";
       };
    }
    
    
    
    var decorators=Array(new SeeButtonDecorator(),new OpenButtonDecorator());
    for (var decorator in decorators){
        document.writeln(decorators[decorator].build());
    }
    

    A method dispatch occurs this way:

    • Does the instance have the method invoked?
      • No -> Delegate call to the parent (it’s prototype) and repeat.
      • Yes-> Execute method body in the context of the implicit object (the one that received the call in the beginning).

    So, when calling new SeeButtonDecorator().build(), first, it will try to execute build method on the instance. As it’s not defined in the instance, method invocation will be delegated to the instance parent, which in this case SeeButtonDecorator prototype, this one, hasn’t got the method neither, so it will delegate the call to it’s parent (PropertyDecorator). PropertyDecorator, has the build() method.

    function PropertyDecorator()
    {
       this.build = function()
       {
          var decoration=this.decorate();
          return "The decoration I did: "+decoration;
       };
    }
    

    When executing it, build method’s body will be evaluated in the context of the new SeeButtonDecorator(). The instance itself won’t have a decorate() method, as it’s defined in SeeButtonDecorator() function (its prototype). Well, this time the call will be delegated to the instance prototype, wich will finally got a decorate() method:

    function SeeButtonDecorator()
    {
       this.decorate = function()
       {
          return "see button";
       };
    }
    

    The method will be executed in the context of the instance again, and will return the string, falling back in the call stack until returning

    The decoration I did: see button
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long snippet of HTML I want some javascript variable to equal
How to implement timeout in this code: Response.Write(@<script language='javascript'>alert('some alert');</script>); Response.Redirect(Request.ApplicationPath); I want to
I want to implement a time counter with JavaScript or PHP. I did some
I'm writing some JavaScript to implement placeholder text in browsers that don't have it.
I want to implement my own clustering algorithm using this Virtual Earth javascript API:
I want to implement dynamic, client side file generation in javascript. Is it possible?
I want to use javascript to insert some elements into the current page. Such
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I have an HTML file and I want to use javascript to call a
I want to implement the SRX Segmentation Rules using javascript to extract sentences from

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.