server-side javascript - classic asp

情到浓时终转凉″ 提交于 2019-12-11 01:42:15

问题


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));
}

回答1:


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.




回答2:


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.




回答3:


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]



回答4:


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.



来源:https://stackoverflow.com/questions/4402057/server-side-javascript-classic-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!