eval
1 отговора
76 прегледа
Re: eval
4uda se sto ti otgovarqm na vyprosa, vse pak e hubavo predi da se postvat nekvi vyprosi 4oveka da si e potyrsil nqkakyv otgovor, to ne moje vse na gotovo, nadali ste ti byda mnogo polezen nop ste ti pastna ne6to direktno ot JS reference!
eval
Evaluates a string of JavaScript code without reference to a particular object.Core function
Implemented in
JavaScript 1.0
ECMA version
ECMA-262
Syntax
eval(string)
Parameters
string
A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.
Description
eval is a top-level function and is not associated with any object.
The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.
If the argument of eval is not a string, eval returns the argument unchanged. In the following example, the String constructor is specified, and eval returns a String object rather than evaluating the string.
eval(new String("2+2")) // returns a String object containing "2+2"eval("2+2") // returns 4
You should not indirectly use the eval function by invoking it via a name other than eval. For example, you should not use the following code:
var x = 2var y = 4var myEval = evalmyEval("x + y")
Backward Compatibility
JavaScript 1.1. eval is also a method of all objects. This method is described for the Object class.
Examples
The following examples display output using document.write. In server-side JavaScript, you can display the same output by calling the write function instead of using document.write.
Example 1. In the following code, both of the statements containing eval return 42. The first evaluates the string "x + y + 1"; the second evaluates the string "42".
var x = 2var y = 39var z = "42"eval("x + y + 1") // returns 42eval(z) // returns 42
Example 2. In the following example, the getFieldName(n) function returns the name of the specified form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.
var field = getFieldName(3) document.write("The field named ", field, " has value of ", eval(field + ".value"))
Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0 to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.
var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "document.write("<P>z is ", eval(str))
Example 4. In the following example, the setValue function uses eval to assign the value of the variable newValue to the text field textObject:
function setValue (textObject, newValue) { eval ("document.forms[0]." + textObject + ".value") = newValue}
Example 5. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.
function Dog(name,breed,color) { this.name=name this.breed=breed this.color=color}myDog = new Dog("Gabby")myDog.breed="Lab"var breed='Shepherd'document.write("<P>" + eval('breed'))document.write("<BR>" + myDog.eval('breed'))
See also
Object.eval method
You can run, but you can't hide!
eval
Evaluates a string of JavaScript code without reference to a particular object.Core function
Implemented in
JavaScript 1.0
ECMA version
ECMA-262
Syntax
eval(string)
Parameters
string
A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects.
Description
eval is a top-level function and is not associated with any object.
The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.
If the argument of eval is not a string, eval returns the argument unchanged. In the following example, the String constructor is specified, and eval returns a String object rather than evaluating the string.
eval(new String("2+2")) // returns a String object containing "2+2"eval("2+2") // returns 4
You should not indirectly use the eval function by invoking it via a name other than eval. For example, you should not use the following code:
var x = 2var y = 4var myEval = evalmyEval("x + y")
Backward Compatibility
JavaScript 1.1. eval is also a method of all objects. This method is described for the Object class.
Examples
The following examples display output using document.write. In server-side JavaScript, you can display the same output by calling the write function instead of using document.write.
Example 1. In the following code, both of the statements containing eval return 42. The first evaluates the string "x + y + 1"; the second evaluates the string "42".
var x = 2var y = 39var z = "42"eval("x + y + 1") // returns 42eval(z) // returns 42
Example 2. In the following example, the getFieldName(n) function returns the name of the specified form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.
var field = getFieldName(3) document.write("The field named ", field, " has value of ", eval(field + ".value"))
Example 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0 to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.
var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "document.write("<P>z is ", eval(str))
Example 4. In the following example, the setValue function uses eval to assign the value of the variable newValue to the text field textObject:
function setValue (textObject, newValue) { eval ("document.forms[0]." + textObject + ".value") = newValue}
Example 5. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.
function Dog(name,breed,color) { this.name=name this.breed=breed this.color=color}myDog = new Dog("Gabby")myDog.breed="Lab"var breed='Shepherd'document.write("<P>" + eval('breed'))document.write("<BR>" + myDog.eval('breed'))
See also
Object.eval method
You can run, but you can't hide!