简体   繁体   中英

server-side javascript - classic asp

In client-side javascript the "this" operator is the window object. What is the "this" operator in classic asp server-side javascript?

In the following code, what is “this” referencing when run in classic ASP server-side ?

test();

function test()
{
    Response.Write(typeof(this));
}

The this object seems to receive special treatment in the global scope of a server-side asp page. In my tests, you can append this. to global objects like Response (as Shadow Wizard suspected):

this.Response.write("foo!");

Works fine. But you cannot reflect on the this object itself. Trying for(var key in this) threw an exception:

An unhandled exception ('Object doesn't support this action') occurred in w3wp.exe [5868].

You get the same exception just for testing the existence of this :

if (this) { ... }

So it is not a normal javascript object at all, and (as Shadow Wizard says) is pretty useless in the global scope.

You mean server side JScript, not JavaScript.

In JScript you don't have any window or "global object" like in client side JavaScript so "this" is pretty much meaningless unless you're inside object or class, then this refers to the instance of that object.

The official documentation explains it pretty well.

The global scope object in Classic ASP JScript is IScriptingContext from asptlb.h . In Classic ASP, this object is not enumerable. The only objects defined on IScriptingContext are:

<%@ Language="Javascript"%>

<%
Response.Write(typeof this.Application + "<br>");
Response.Write(typeof this.Request + "<br>");
Response.Write(typeof this.Response + "<br>");
Response.Write(typeof this.Server + "<br>");
Response.Write(typeof this.Session + "<br>");

Response.Write(Object.prototype.toString.call(this) + "<br>");
%>

which prints:

object
object
object
object
object
[object Object]

this doesn't always point to window .

What is this in the following code?

function Test() {
    var obj = {};
    obj.newFunc = function() { this.value = 42; }
    obj.newFunc(); // "this" is "obj"
    var obj2 = new obj.newFunc(); // Whoa, what's going on? "this" is the new object
}

In client-side JS and server-side JS, this only points to the context object in which a function has been called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM