0

I would like to do the following in JavaScript:

var myObject.name = myString;

function newFunction(){myObject.name}

Is it possible to use a string as the contents of a function? How would you convert it to be usable?

5
  • I'm having trouble understanding what you're trying to do. Functions are supposed to do something whereas strings and other basic types are supposed to be something. Why would you want to make a function which contains only a string? Commented Jan 3, 2012 at 23:58
  • Your intent is unclear here. The code you've posted is valid, but doesn't "do" anything. You could return myObject.name to get the string back out of the function, but unless that's what you're after, we need to know more about what your goal is. Commented Jan 3, 2012 at 23:59
  • I think he wants the string to be the function's body. Like myString = "return x;" -in which case he could use eval() Commented Jan 3, 2012 at 23:59
  • would you like to treat the contents of the string myObject.name as javascript and execute it? if so you could use eval Commented Jan 4, 2012 at 0:00
  • or similarly: var myObject.name = "x=1,y=1" "function(){myObject.name; var myArray = []; myArray = myObject.name; return myArray} Commented Jan 4, 2012 at 0:14

4 Answers 4

2

Ok, I get what you're trying to do. You can do it like this:

var newFunction = new Function(myObject.name);

This means if myObject.name equals "alert('it works')", when you call newFunction() you will be alerted. (new Function(code) is an alternative to eval, specifically for what you're doing.)

But this is considered very bad practice in JavaScript (and in programming in general), as code that can alter itself can quickly become unmanageable, and there's usually a better way to do things. Unless you show us what it is you're doing, I can't say what that better way is.

Sign up to request clarification or add additional context in comments.

1 Comment

I like this approach better than the solutions using eval(). It's a bit cleaner syntactically. +1.
1

You can use eval() to do this, assuming myObject.name contains valid Javascript:

function newFunction(){ eval( myObject.name ); };

Comments

0

I believe you want myString to hold code, right ?

You can execute that code by using the eval()MDN docs method.

function newFunction(){ eval(myObject.name); }

Warning !

be sure to read the Don't use eval! section though ..

Comments

0

JavaScript has the eval() function but that's generally not a function you want to be using...

You'd then write:

var myObject.name = myString;

function newFunction() {
    eval(myObject.name);
}

However, this is generally a dangerous and bad idea - on a higher level, what are you trying to do?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.