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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:24:57+00:00 2026-06-17T19:24:57+00:00

Assuming the presence of Harmony Proxies in the underlying Javascript engine, how could one

  • 0

Assuming the presence of Harmony Proxies in the underlying Javascript engine, how could one construct a CoffeeScript superclass such that extending it would allow a class to define a noSuchMethod method (or methodMessing)?

That method would be called with a name and an argument list if the class (or its superclasses) does not have the method requested.

  • 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-06-17T19:24:58+00:00Added an answer on June 17, 2026 at 7:24 pm

    Nice Question! =D

    (Note: i’ve only tested this in Firefox, as it seems is the only browser that supports Harmony proxies.)

    This seems to work for missing properties:

    class DynamicObject
    
      propertyMissingHandler =
        get: (target, name) ->
          if name of target
            target[name] 
          else
            target.propertyMissing name
    
      constructor: ->
        return new Proxy @, propertyMissingHandler
    
      # By default return undefined like a normal JS object.
      propertyMissing: -> undefined
    
    class Repeater extends DynamicObject
      exited: no
      propertyMissing: (name) ->
        if @exited then "#{name.toUpperCase()}!" else name
    
    r = new Repeater
    console.log r.hi     # -> hi
    console.log r.exited # -> false. Doesn't print "exited" ;)
    r.exited = yes
    console.log r.omg    # -> OMG!
    

    Now, it works, but it has a small big caveat: it relies on an “other typed” constructor. That is, the constructor of DynamicObject returns something else than the DynamicObject instance (it returns the proxy that wraps the instance). Other typed constructors have subtle and not-so-subtle problems and they are not a very loved feature in the CoffeeScript community.

    For example, the above works (in CoffeeScript 1.4), but only because the generated constructor for Repeater returns the result of calling the super constructor (and therefore returns the proxy object). If Repeater would have a different constructor, it wouldn’t work:

    class Repeater extends DynamicObject
      # Innocent looking constructor.
      constructor: (exited = no) ->
        @exited = exited
      propertyMissing: (name) ->
        if @exited then "#{name.toUpperCase()}!" else name
    
    console.log (new Repeater yes).hello # -> undefined :(
    

    You have to explicitly return the result of calling the super constructor for it to work:

      constructor: (exited = no) ->
        @exited = exited
        return super
    

    So, as other typed constructors are kinda confusing/broken, i’d suggest avoiding them and using a class method to instantiate these objects instead of new:

    class DynamicObject
    
      propertyMissingHandler =
        get: (target, name) ->
          if name of target
            target[name] 
          else
            target.propertyMissing name
    
      # Use create instead of 'new'.
      @create = (args...) ->
        instance = new @ args...
        new Proxy instance, propertyMissingHandler
    
      # By default return undefined like a normal JS object.
      propertyMissing: -> undefined
    
    class Repeater extends DynamicObject
      constructor: (exited = no) ->
        @exited = exited
        # No need to worry about 'return'
      propertyMissing: (name) ->
        if @exited then "#{name.toUpperCase()}!" else name
    
    console.log (Repeater.create yes).hello # -> HELLO!
    

    Now, for missing methods, in order to have the same interface as requested in the question, we can do something similar in the proxy handler, but instead of directly calling a special method (propertyMissing) on the target when it has no property with that name, it returns a function, that in turn calls the special method (methodMissing):

    class DynamicObject2
    
      methodMissingHandler =
        get: (target, name) ->
          return target[name] if name of target
          (args...) ->
            target.methodMissing name, args
    
      # Use this instead of 'new'.
      @create = (args...) ->
        instance = new @ args...
        new Proxy instance, methodMissingHandler 
    
      # By default behave somewhat similar to normal missing method calls.
      methodMissing: (name) -> throw new TypeError "#{name} is not a function"
    
    class CommandLine extends DynamicObject2
      cd: (path) ->
        # Usually 'cd' is not a program on its own.
        console.log "Changing path to #{path}" # TODO implement me
      methodMissing: (name, args) ->
        command = "#{name} #{args.join ' '}"
        console.log "Executing command '#{command}'" 
    
    cl = CommandLine.create()
    cl.cd '/home/bob/coffee-example'  # -> Changing path to /home/bob/coffee-example
    cl.coffee '-wc', 'example.coffee' # -> Executing command 'coffee -wc example.coffee'
    cl.rm '-rf', '*.js'               # -> Executing command 'rm -rf *.js'
    

    Unfortunately, i couldn’t find a way to distinguish property accesses from method calls in the proxy handler so that DynamicObject could be more intelligent and call propertyMissing or methodMissing accordingly (it makes sense though, as a method call is simply a property access followed by a function call).

    If i had to choose and make DynamicObject as flexible as possible, i’d go with the propertyMissing implementation, as subclasses can choose how they want to implement propertyMissing and treat that missing property as a method or not. The CommandLine example from above implemented in terms of propertyMissing would be:

    class CommandLine extends DynamicObject
      cd: (path) ->
        # Usually 'cd' is not a program on its own.
        console.log "Changing path to #{path}" # TODO implement me
      propertyMissing: (name) ->
        (args...) ->
          command = "#{name} #{args.join ' '}"
          console.log "Executing command '#{command}'" 
    

    And with that, we can now mix Repeaters and CommandLines that inherit from the same base class (how useful! =P):

    cl = CommandLine.create()
    r = Repeater.create yes
    cl.echo r['hello proxies'] # -> Executing command 'echo HELLO PROXIES!'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming a javascript-like language, how can we implement a system such as: a =
Assuming that I have this: enum { A = 0x2E, B = 0x23, C
Assuming I have a ASP.NET MVC 3 application that runs in a web farm
Assuming that parsing the equation would not be a problem, how can I make
Assuming that the larger a database gets, the longer it will take to SELECT
Assuming that I had two Models called User and Comment And there is a
Assuming that a memory address occupies 4 bytes and a char occupies 1 byte:
Assuming I have an instance of an object that I know belongs to a
Assuming that I have the following code: final Catalog catalog = createCatalog(); for (int
Assuming I want the newest flavour of visualstudio that is able to open C#

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.