JavaScript,函式功能,函式原型,函式說明,實例,Matlab,Function,Syntax,Description,Remarks,Examples,PHP,函式功能,實例,VBScript,函式功能,實例,Python,函式功能,語法,實例,
JavaScript
函式功能
eval() 函式可將
字元串轉換為代碼執行,並返回一個或多個值
函式原型
返回值 = eval( codeString )
函式說明
如果eval函式在執行時遇到錯誤,則拋出異常給
調用者.
類似的函式是loadcode ,loadcode並不立即執行代碼,而是返回一個函式對象.
並且loadcode支持路徑參數,eval並不支持. eval並不支持代碼中的return語句,而是將代碼作為表達式直接計算出結果.
實例
var d = eval("({name:'chentong'})")
alert(d.name);
Matlab
Function
Execute string containing MATLAB expression
Syntax
eval(expression)
[a1, a2, a3, ...] = eval('myfun(b1, b2, b3, ...)')
Description
eval(expression) executes expression, a string containing any valid MATLAB expression. You can construct expression by concatenating substrings and variables inside square brackets:
expression = [string1, int2str(var), string2, ...]
[a1, a2, a3, ...] = eval('myfun(b1, b2, b3, ...)') executes function smyfun with arguments b1, b2, b3, ..., and returns the results in the specified output variables.
Remarks
Using the eval output argument list is recommended over including the output arguments in the expression string. The first syntax below avoids strict checking by the MATLAB parser and can produce untrapped errors and other unexpected behavior. Use the second syntax instead:
% Not recommended
eval('[a1, a2, a3, ...] = function(var)')
% Recommended syntax
[a1, a2, a3, ...] = eval('function(var)')
Examples
Example 1 – Working with a Series of Files
Load MAT-files August1.mat to August10.mat into the MATLAB workspace:
for d=1:10
s = ['load August' int2str(d) '.mat']
eval(s)
end
These are the strings being evaluated:
s =
load August1.mat
s =
load August2.mat
s =
load August3.mat
- etc. -
Example 2 – Assigning to Variables with Generated Names
Generate variable names that are unique in the MATLAB workspace and assign a value to each using eval:
for k = 1:5
t = clock;
pause(uint8(rand * 10));
v = genvarname('time_elapsed', who);
eval([v ' = etime(clock,t)'])
end
As this code runs, eval creates a unique statement for each assignment:
time_elapsed =
5.0070
time_elapsed1 =
2.0030
time_elapsed2 =
7.0010
time_elapsed3 =
8.0010
time_elapsed4 =
3.0040
Example 3 – Evaluating a Returned Function Name
The following command removes a figure by evaluating its CloseRequestFcn property as returned by get.
eval(get(h,'CloseRequestFcn'))
PHP
函式功能
Eval函式在PHP代碼中的使用:eval() 函式把字元串按照 PHP 代碼來計算。該字元串必須是合法的 PHP 代碼,且必須以分號結尾。如果沒有在代碼字元串中調用 return 語句,則返回 NULL。如果代碼中存在解析錯誤,則 eval() 函式返回 false。實例如圖所示。
實例
$string = "beautifual";$time = "winter";$str =' This is a $string $time morning!'; echo $str. "<br />";eval("\$str = \"$str\";");echo $str;
VBScript
函式功能
Eval函式在VBScript腳本語言中的使用: 在VB腳本語言中,Eval函式具有兩層意思,一是實現計算表達的值,即eval()函式可將字元串轉換為代碼執行,並返回一個或多個值;二是運行指定的代碼。
實例
dim a,b,sum;a = 10;b = 20;sum = 25;magbox "10+20="& eval("a+b")msgbox "25 = (10+20)?" & eval("sum=a+b")
Python
函式功能
eval通常用來執行一個字元串表達式,並返回表達式的值。
語法
eval(expression[, globals[, locals]])
有三個參數,表達式字元串,globals變數作用域,locals變數作用域。 其中第二個和第三個參數是可選的。
如果忽略後面兩個參數,則eval在當前作用域執行。
實例
>>> a=1>>> eval("a+1")2>>>
如果指定globals參數
>>> a=1>>> g={'a':10}>>> eval("a+1",g)11>>>
如果指定locals參數
>>> a=10>>> b=20>>> c=20>>> g={'a':6,'b':8}>>> l={'b':9,'c':10}>>> eval("a+b+c",g,l)25>>>
如果要嚴格限制eval執行,可以設定globals為__builtins__,這樣 這個表達式只可以訪問__builtin__module。