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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:17:14+00:00 2026-06-10T11:17:14+00:00

How should I get this to work ? var param = ‘someFunction’; require(‘views/MyView’).[param](); When

  • 0

How should I get this to work ?

var param = 'someFunction';

require('views/MyView').[param]();

When I run this code, I get the following error

SyntaxError: missing name after . operator
require('views/MyView').[renderMethod]();
  • 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-10T11:17:16+00:00Added an answer on June 10, 2026 at 11:17 am

    . and [] are kind of the same thing.

    What you want is require('views/MyView')[renderMethod]();

    require(...) will return the exported module, which should basically be an object with the functions as properties.

    So, let’s say we have:

    var obj = {
       foo: function() { alert('foo'); }
       bar: function() { alert('bar'); }
    }
    

    Then you could do:

    obj.foo(); – call a fixed name function

    obj['foo'](); – dynamic name fixed arguments

    obj['foo'].apply(this, args) – dynamic function name and arguments.

    Edit:

    One more thing I noticed:

    In requirejs, when you do something like this:

    define (require, function() {
        x = require('views/Foo');
        x.bar();
    }
    

    RequireJS will determine by parsing the code that you will need the ‘view/Foo’ module, and make sure it is loaded before executing your code.

    But if you want to load a dynamic module, it won’t know beforehand what module to preload, so you will have to use a callback to be notified when your module will be loaded:

    define (require, function() {
    
        require('views/' + viewName, function(myView) {
            myView.bar(); // or 
            myView[dynamicFunc](); // ... 
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still not sure I totally get how this particular case should work out.
This should be simple, yet I can't get it to work. I have a
This should be a simple question, but I can't get it to work :(
what am I doing wrong in this code.. I should get the class name
This is a snippet of a user invite code. The code should get the
According to JavaScript Patterns book (p. 79), this should work: var ob = {
I haven't been able to get something like this to work: var myWorker =
I want to get a window like this How should i get this and
i get this error when trying this: ERROR method name expected. How should i
I can't seem to get this working, what should happen is when the user

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.