A solution to the problem that eval cannot define variables in ES0en. net

  • 2020-09-16 07:25:59
  • OfStack

 
eval.asp 
<%@ LANGUAGE='JAVASCRIPT'%> 
<script Language="javascript" runat=server> 
eval("var f1=1,f2=2,f3=3;"); 
Response.Write(f1+"<br/>"); 
Response.Write(f2+"<br/>"); 
Response.Write(f3+"<br/>"); 
</script> 
 Operation results:  
1 
2 
3 
eval01.aspx 
<%@ LANGUAGE='JAVASCRIPT' DEBUG="true"%> 
<script Language="javascript" runat=server> 
eval("var f1=1,f2=2,f3=3;"); 
Response.Write(f1+"<br/>"); 
Response.Write(f2+"<br/>"); 
Response.Write(f3+"<br/>"); 
</script> 
 The first 3 The line has a syntax error ! 
 It can be done through 1 The method of dynamically adding attributes to empty objects solves the problem of defining variables according to table fields:  
 The name of the table :T 
F1 int, 
F2 char(10), 
F3 datetime 
asp: 
eval( " var F1='',F2='',F3=''; " ); 
Response.Write( " F1= " +F1); 
aspx: 
var T={}; 
T[ " F1 " ]= "" ; 
T[ " F2 " ]= "" ; 
T[ " F3 " ]= "" ; 
Response.Write( " F1= " +T.F1); 
<%@ LANGUAGE='JScript' DEBUG="true"%> 
<% 
var TAB={ 
}; 
var n=12; 
var FLD; 
for(var i=0;i<n;i++) 
{ 
FLD= "F0"+((i<10)?("00"):((i<100)?("0"):("")))+i; 
TAB[FLD]=i+1000; 
} 
for(var i=0;i<n;i++) 
{ 
FLD= "F0"+((i<10)?("00"):((i<100)?("0"):("")))+i; 
Response.Write(TAB[FLD]+"<br/>"); 
} 
%> 

Description of Jscript8.0 on Microsoft's official website:

http://msdn.microsoft.com/zh-cn/library/8e4z2w8w(v=vs.90).aspx#jsconupgradingapplicationcreatedinpreviousversionsofjscriptanchor7

Upgrade the application created in the previous version of JScript

Visual Studio 2008

Other versions

2010 · Visual Studio

2005 · Visual Studio

This topic has not been rated - evaluate this topic

Update: November 2007

Most of the existing JScript code makes good use of the enhancements included in JScript 8.0 because JScript 8.0 is almost completely backward compatible with previous versions. JScript 8.0 breaks new ground with its new features.

By default, the JScript 8.0 program is compiled in fast mode. Because fast mode has some restrictions on the types of code allowed, programs can be more efficient and execute faster. However, some of the features available in previous releases are not available in fast mode. Most of these features are incompatible with multithreaded applications and can make code inefficient. For programs compiled with a command-line compiler, you can turn off fast mode and take advantage of full backward compatibility. Note that the code compiled this way runs more slowly and is less fault tolerant. Fast mode cannot be turned off in ASP.NET applications because of stability issues. For more information, see /fast.

Fast mode

In fast mode, the following JScript behavior is triggered:

· All variables must be declared.

· The function becomes constant.

· Internal objects cannot have the expando attribute.

· Properties of internal objects cannot be listed or changed.

· The arguments object is not available.

· You cannot assign values to read-only variables, fields, or methods.

· The eval method cannot define an identifier within a enclosing scope.

· The eval method executes the script in a restricted security context.

All variables must be declared

Previous versions of JScript did not require variables to be explicitly declared. While this feature saves programmers keystrokes, it also makes it difficult to track down errors. For example, you might assign a variable name to a misspelled variable name, which would neither generate an error nor return the desired result. Moreover, undeclared variables have global scope and can cause other confusion.

Fast mode requires that declared variables be displayed. This helps avoid the various errors mentioned earlier and results in faster code.

JScript.NET also supports type-annotated variables. This binds each variable to a specific data type, which can only store that type of data, starting at 1. Although type annotation is not required, using it helps avoid those errors associated with accidentally storing error data in variables and increases the speed of program execution.

For more information, see JScript variables and constants.

The function becomes a constant

In previous VERSIONS of JScript, functions declared with function statements were treated the same as variables that held Function objects. In particular, any function identifier can be used as a variable to store any type of data.

In fast mode, the function becomes a constant. Therefore, a function cannot be assigned a new value or redefined. This avoids accidentally changing the meaning of the function.

If your script needs to make a function change, you can explicitly use a variable to hold an instance of the Function object. Note, however, that the Function object moves slowly. For more information, see Function objects.

An internal object cannot have the expando attribute

In previous VERSIONS of JScript, you could add expando attributes to internal objects. For example, this behavior can be used to add methods to the String object to crop the whitespace before the string.

In fast mode, this is not allowed. If your script USES this feature, you must modify the script. Instead of attaching those functions as methods to an object, functions can be defined globally. Each instance in the script, where the expando method is called from the object, is then overwritten to pass the object to the appropriate function.

An important exception to this rule is the Global object, which can still have the expando attribute. All globally scoped modifiers are actually properties of the Global object. Obviously, the Global object must be dynamically extensible to support the addition of new global variables.

You cannot list or change the properties of an internal object

In previous versions of JScript, predefined properties of internal objects could be deleted, enumerated, or written to. For example, this behavior can be used to change the default toString method of an Date object.

In fast mode, this is not allowed. Since internal objects cannot have the expando attribute, this functionality is no longer required, and the attributes for each object are listed in the reference section. For more information, see Objects.

The arguments object is not available

Previous VERSIONS of JScript provided an arguments object in the function definition that allowed the function to take any number of arguments. The parameter object can also refer to the current function and the calling function.

In fast mode, the arguments object is not available. However, JScript 8.0 allows function declarations to specify an array of parameters in the function argument list. This allows the function to take any number of arguments, replacing some of the functionality of the arguments object. For more information, see the function statement.

In fast mode, there is no way to directly access and reference the current function or call the function.

You cannot assign values to read-only variables, fields, or methods

In previous versions of JScript, statements seemed to assign values to read-only identifiers. This assignment will fail without warning, and the only way to find an assignment failure is to test whether the value has actually changed. Assigning a read-only identifier is usually the result of some kind of error, because it doesn't have any effect.

In fast mode, if you try to assign a read-only identifier, a compile-time error is generated. You can either remove the assignment, or you can try to assign an identifier that is not read-only.

If fast mode is turned off, the assignment to a read-only identifier will fail at run time without prompting, but a compile-time warning will be generated.

The eval method cannot define an identifier within a enclosing scope

In previous versions of JScript, functions and variables could be defined locally or globally by calling the eval method.

In fast mode, functions and variables can be defined in a call to the eval method, but they can only be accessed from that particular call. Once eval is completed, functions and variables defined within eval are no longer accessible. The results calculated within eval can be assigned to any variable that is accessible in the current range. Calls to eval methods are slow, and you should consider rewriting the code that contains them.

When fast mode is turned off, the previous behavior of the eval method is restored.

The eval method executes the script in a restricted security context

In previous versions of JScript, the code passed to the eval method would run in the same 1 security context as the calling code.

To protect the user, the code passed to the eval method is executed in the restricted security context, unless the string "unsafe" is passed as the second argument. Restricted security contexts prohibit access to system resources, such as file systems, networks, or user interfaces. If your code attempts to access these resources, a security exception is raised.

When the second argument to eval is the string "unsafe", the code passed to the eval method is executed in the security context of the calling code. In this way, you can restore the previous behavior of the eval method.

Safety instructions:

Using eval in non-secure mode can only execute code strings obtained from known sources

Related articles: