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 6702129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:01:03+00:00 2026-05-26T07:01:03+00:00

Can anyone provide a specific example showing Javascript’s prototypal inheritance that demonstrates when it’s

  • 0

Can anyone provide a specific example showing Javascript’s prototypal inheritance that demonstrates when it’s beneficial to use over a traditional class-based (classical) model?

Other questions I have seen (such as Classical Vs prototypal inheritance, Why was JavaScript implemented using prototypal inheritance?, prototype based vs. class based inheritance) only give a high level discussion, not a specific example (preferably one that you have used in production code).

  • 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-26T07:01:03+00:00Added an answer on May 26, 2026 at 7:01 am

    I think the biggest reason that we don’t see more of the prototypal pattern directly is that the default syntax in Javascript is that pseudo-classical aberration instead of the more favourable Object.create. If you want to really see the prototypal patter shining search for places where this function is used. The following example comes from the Dojo toolkit:


    Caveat: I kind of changed my mind about how good this kind of code is since back when I originally wrote this answer. While the basic idea still stands, you should be careful it you have methods that mutate instance (“this”) properties. This is because if you invoke a method in the delegate object via the delegator then you might end up setting variables in the delegator instead of in the delegate and that might break some invariants if someone else ends up accessing the delegate directly latter on.

    The whole idea is 100% fine you you have immutable objects though.


    Dojo defines a general store interface (with methods like get(), add(), etc) that can be used, for example, to abstract a REST API from the server. We would like to create a Cache function that receives any datastore and returns a new version that caches any calls to the get() method (this allows us to decouple the caching from the store-specific behaviour of implementing the actual get())

    A first idea would involve using the fact the Javascript is highly dinamic to replace the get method:

    //not the actual implementation. Things get more complicated w/ async code.
    var oldGet = store.get;
    store.get = function(id){
        if(!cache[id]){ cache[id] = oldGet(id); }
        return cache[id];
    }
    

    However, this clobbers the original object so you can’t access the original method anymore and also makes it trickier to add other modifications in parallel.

    A second idea would be to make a more robust solution using delegation:

    function Cache(obj){
        this.obj = obj;
    }
    Cache.prototype = {
        get: function(){
            //do things involving this.obj...
        }
    };
    

    This looks promising, until you remember that the resulting Cache object needs to implement the store interface. We could try adding all the methods by hand:

    Cache.prototype = {
        //...
        put: function(){ return this.obj.apply(this, arguments); },
        //...
    }
    

    but not only would that be cumbersome and error prone (its so easy to forget something) it would not even be as powerful as the object-modifying solution since we lose access to methods in that original object that aren´t from the store interface.

    Well, the way to do this kind of “automatic delegation” is inheritance, but it would initialy seem useless in this case since you would need to create a new cache subclass for every possible store class or you would need some sort of fancy multiple-inheritance mixin. Enter prototypal inheritance to save the day. We can easily make a new obejct that adds functionality to an old one without modifying it or having to fiddle with the class hieharchies

    dojo.store.Cache = function(masterStore, cachingStore, options){
        //...
        return dojo.delegate(masterStore, {
            //... 
            get: function(id, directives){
                //...
            }
            //...
        }
    }
    

    Where dojo.delegate is a function that creates a new object with all the properties in the second argument and whose prototype will be the first argument.


    Non JS theoretical ramblings: Prototypal inheritance can be used even more agressively in even more delegation scenarios in a language like Self that allows multiple prototypes and also direct access and modification of prototypes at runtime. For example, it is possible to implement the State pattern from the GoF by delegating all suitable methods to a prototype and changing the prototype whenever the state changes.

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

Sidebar

Related Questions

Can anyone provide me an example of how to use WM_CLOSE to close a
Can anyone provide a clear explanation / example of what these functions do, and
Can anyone provide me with a hello world example for a major mode in
Can anyone recommend any good material that seeks to provide a real world perspective
You can use a device modifier (i.e., ~ipad) to provide a device-specific key in
I am using prototypal inheritance in JavaScript and have hit an issue I can't
Can anyone provide some real examples as to how best to keep script files
Can anyone provide some pseudo code for a roulette selection function? How would I
Can anyone provide me with a link to good article about using Firebird.NET with
Can anyone provide some links to good information on setting up Silverlight 2.0 to

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.