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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:59:47+00:00 2026-05-28T13:59:47+00:00

How do I instantiate a class in CoffeeScript when I only have the name

  • 0

How do I instantiate a class in CoffeeScript when I only have the name of the class in a string?

class Dog
  bark:->
    "Woof"

className = "Dog"
dog = new className  # <--- I would like to create an instance here using the string
  • 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-28T13:59:47+00:00Added an answer on May 28, 2026 at 1:59 pm

    With a bit of foresight you can do this pretty easily and protect yourself against using eval. Keep a list somewhere of the classes that you want to instantiate by name:

    class Dog
      bark:->
        "Woof"
    
    # Or window.named_classes if you need to access
    # `named_classes` globally (or just in another
    # CoffeeScript file).
    named_classes = { Dog: Dog }
    

    and then use your lookup table instead of eval:

    name = 'Dog'
    dog  = new named_classes[name]
    

    When you say class Dog, you end up with a local variable called Dog:

    var Dog;
    Dog = (function() { /* ... */ })();
    

    and there’s no way to get at a local JavaScript variable unless you store it somewhere that you can access by name. Also note that eval won’t work if you define Dog in one CoffeeScript file and want to access it another, CoffeeScript wraps each file in self-executing anonymous function to limit variable scope:

    all CoffeeScript output is wrapped in an anonymous function: (function(){ ... })(); This safety wrapper, combined with the automatic generation of the var keyword, make it exceedingly difficult to pollute the global namespace by accident.

    If you’d like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS.

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

Sidebar

Related Questions

I'd like to instantiate a class but I only have its name in a
I'd like to instantiate a class by name (a string) without specifying a namespace
Is there a way to instantiate objects from a string holding their class name?
How can I instantiate a class by throwing in a variable name? Consider this
Is it possible to assure that only spring can instantiate a class, and not
ADO.NET has the notorious DataRow class which you cannot instantiate using new. This is
I'm trying to create a class that can instantiate arrays at runtime by giving
Duplicate: Is there a way to instantiate objects from a string holding their class
After creating a metaclass using Moose::Meta::Class->create , how do I instantiate a real Moose
So normal class methods and object creation goes like this... $obj = new Class();

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.