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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:02:12+00:00 2026-06-13T19:02:12+00:00

So basically I have many functions that are very similar but just with slightly

  • 0

So basically I have many functions that are very similar but just with slightly different function names and accessing slightly different variables.

Instead of repeating myself I want to create these methods via something similar to define_method in Ruby.

Anyone know how to do this in an Ember.js object? Oh and preferably CoffeeScript!

This is obviously wrong but just a very basic example.

Thing = Ember.Object.extend()

animal = "cow"
say = "moo"

animal = "dog"
say = "woof"

Thing.reopenClass(
this["#{animal}Speak"]: ->
console.log say
)

Can anyone help with this?

  • 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-13T19:02:14+00:00Added an answer on June 13, 2026 at 7:02 pm

    reopenClass just needs an object so you can build the object and then hand it to reopenClass:

    add_this = { }
    add_this["#{animal}Speak"] = -> console.log say
    Thing.reopenClass add_this
    

    Unfortunately you can’t use string interpolation when building a key for an object literal so the obvious (but incorrect):

    Thing.reopenClass(
        "#{animal}Speak": -> console.log say
    )
    

    gives you a syntax error.

    I think the root of the problem is that a string with interpolation is not a string literal, it is an expression whose value is a string. The keys in an object literal must be simple unquoted labels (label:) or string literals ('string': or "string":). When you say this:

    "string with #{interpolation}"
    

    you’re really just using a shorthand for this:

    "string with " + interpolation
    

    and the CoffeeScript compiler isn’t smart enough to rewrite this:

    o = { "string with #{interpolation}": value }
    

    as this JavaScript:

    o = { }
    o['string with ' + interpolation] = value
    

    So you can’t use string valued expressions as keys for object literals in CoffeeScript (or JavaScript).

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

Sidebar

Related Questions

In my app, I have many different functions. These are view controllers that are
I have found many people with simliar issues but no soultions...basically I have two
Basically, I have a model where I've created a superclass that many other classes
this question is very similar to many other questions posted on stack overflow,I have
Basically, I have a string of functions that load a set of blog entries
I have a method for validation that has many conditional statements. Basically it goes
I basically have a program that filters records from one excel file to another
I basically have 7 select statements that I need to have the results output
what I mean by that is: I basically have a class that has too
I am very new to javascript and hit a wall. Basically I have a

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.