The solution to the problem that eval cannot define variables in asp.net-practical tips

Source: Internet
Author: User
Tags function definition visual studio visual studio 2010
Copy Code code as follows:

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>
Run Result:
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 third line has a syntax error!
You can resolve the problem of defining a variable by a table field by dynamically adding a property to an empty object:
Table Name: 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)? (" ):((i<100)? ("0"):(")) +i;
tab[fld]=i+1000;
}
for (Var i=0;i<n;i++)
{
Fld= "F0" + (i<10)? (" ):((i<100)? ("0"):(")) +i;
Response.Write (tab[fld]+ "<br/>");
}
%>

Microsoft's official website on the Jscript8.0 description:

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

To upgrade an application created in a previous version of JScript

Visual Studio 2008

Other versions

· Visual Studio 2010

· Visual Studio 2005

This topic has not been rated-evaluate this topic

Updated: November 2007

Most of the existing JScript code makes good use of the enhancements that are included in JScript 8.0, because JScript 8.0 is almost completely backward-compatible for previous versions. The new features of JScript 8.0 create a New world.

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

Quick Mode

In quick mode, the following JScript behavior is triggered:

· All variables must be declared.

· The function becomes a constant.

· Internal objects cannot have expando properties.

· You cannot list or change the properties of an internal object.

· The arguments object is not available.

· You cannot assign a value to a read-only variable, field, or method.

· The Eval method cannot define identifiers within the enclosing scope.

· The Eval method executes the script in the restricted security context.

All variables must be declared

A previous version of JScript does not require an explicit declaration of a variable. Although this feature saves the programmer the number of keystrokes, it makes tracking errors difficult. For example, you might assign a value to a misspelled variable name, which would neither generate an error nor return the desired result. Also, undeclared variables have global scope and can cause other confusion.

Quick mode requires a declaration variable to be displayed. This helps to avoid the various errors mentioned earlier and can produce code that runs faster.

JScript. NET also supports variables with type annotations. This binds each variable to a specific data type, which can store only that type of data. Although type annotations are not required, they can be used to help avoid errors associated with accidentally storing error data in variables and to increase the speed at which programs are executed.

For more information, see JScript variables and constants.

function becomes constant

In previous versions of JScript, functions declared with function statements were treated as equals to variables that hold function objects. In particular, any function identifier can be used as a variable to store any type of data.

In quick mode, the function becomes a constant. Therefore, you cannot assign new values or redefine functions for a function. 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 functions object. Note, however, that the Function object is slow to transport. For more information, see Function objects.

internal object cannot have expando property

In previous versions of JScript, you could add expando properties to internal objects. For example, this behavior can be used to add a method to a string object to trim the space before the string.

In quick mode, this is not allowed. If your script uses this feature, you must modify the script. You can define functions globally, rather than attaching those functions to objects as methods. Then, rewrite each instance in the script (in which the expando method is invoked from the object) to pass the object to the appropriate function.

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

cannot list or change properties of internal objects

In previous versions of JScript, you could delete, enumerate, or write to the predefined properties of an internal object. For example, this behavior can be used to change the default ToString method for a Date object.

In quick mode, this is not allowed. Because internal objects cannot have expando properties, this feature is no longer required, and the properties of each object are listed in the reference section. For more information, see objects.

arguments object is not available

Previous versions of JScript provide a arguments object in the function definition that allows a function to accept any argument. The Parameter object can also reference the current function and the calling function.

In quick 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 accept any number of arguments, replacing some of the functionality of the arguments object. For more information, see function statements.

There is no way to directly access and reference the current function or call functions in quick mode.

cannot assign a value to a read-only variable, field, or method

In previous versions of JScript, statements seemed to be able to assign values to read-only identifiers. This assignment will fail silently, and the only way to find the assignment to fail is to test whether the value actually changed. Assigning a value to a read-only identifier is usually caused by an error because it does not have any effect.

In quick mode, if you attempt to assign a value to a read-only identifier, a compile-time error is generated. Either you can remove the assignment or you can try to assign a value to an identifier that is not read-only.

If quick mode is turned off, assigning a value to a read-only identifier will fail silently at run time, but a compile-time warning will be generated.

The Eval method cannot define identifiers within the enclosing scope

In previous versions of JScript, functions and variables can be defined locally or globally by invoking the Eval method.

In quick mode, functions and variables can be defined in the call to the Eval method, but they are accessible only from this particular invocation. Once the eval has been completed, functions and variables defined within the eval can no longer be accessed. The computed results within the eval can be assigned to any variable that is accessible in the current scope. Calls to the Eval method are slow, and you should consider overriding the code that contains those calls.

The previous behavior of the Eval method can be restored when quick mode is turned off.

The Eval method executes a script in a restricted security context

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

To protect the user, the code passed to the Eval method executes 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 generated.

When the second parameter of eval is the string "unsafe", the code passed to the Eval method executes in the security context in which the calling code resides. In this way, the previous behavior of the Eval method can be restored.

Security Note:

Using eval in unsafe mode can only execute code strings obtained from known sources
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.