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

  • Home
  • SEARCH
  • 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 602091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:47:06+00:00 2026-05-13T16:47:06+00:00

I am trying to define a dynamic variable within a function in Rhino JavaScript

  • 0

I am trying to define a dynamic variable within a function in Rhino JavaScript (specifically what is embedded in Java 6), but I don’t want to resort to eval, and I don’t want to have to reference it via this. Basically, I want to take an object, and turn every property into a var within the scope of a function… something like:

var abc = "value";
var context = { abc: 123, xyz: "def" };

function test(cx) {
  for (var p in cx) {
    this_scope[p] = cx[p];
  }

  println(abc);
  // DON'T WANT TO HAVE TO DO THIS:
  // pritnln(this.abc);
}

test(context); // prints: 123
println(abc); // prints: value

Believe it or not, it is significant if I have to use “this.” (it is a dynamically generated function that I want to invoke over and over with different context objects and using “this” for every variable would be very detrimental).

I also want to avoid having to grab a new engine context or something like that… it would be superb if I can do this straight in JavaScript (I think the result would be significantly faster).

  • 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-13T16:47:07+00:00Added an answer on May 13, 2026 at 4:47 pm

    So, basically you want something that works like JS’s with? *grins, ducks, and runs*

    var abc = "value";
    var context = {abc: 123, xyz: "def"};
    
    function test(cx) {
        with (cx) {
            println(abc);
        }
    }
    
    test(context);
    println(abc);
    

    Mind you, some well-known JS practitioners, like Doug Crockford, strongly deprecate the use of with.

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

Sidebar

Related Questions

I'm trying to define a very simple function in Drools as below: import java.util.List;
I'm trying to use dynamic variable names. So inside this loop I want to
I am trying to define a dynamic var in a different namespace. The Lobos
I am trying to define nested modules, but I couldn't find any complete explanations
I'm trying to define a new array through a where query but I can
I am trying to define MapPageRoute on the application global.asax but my problem is
I am trying to define a dynamic array containing char pointers that point to
I am trying to figure out how to define a function that works on
I'm trying to create a dynamic web project in eclipse, but my version of
JavaScript has lexical scoping which means that non-local variables accessed from within a function

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.