I was studying C++ recently and saw function pointers. Since I have been working on ASP before, I wondered if there was such a function pointer in ASP. I searched on Baidu and found that there wasn’t much introduction about this aspect, so I looked through the VBScript manual and couldn’t find it. I was disappointed and found the GetRef function. I quoted the description of the GetRef function in the manual.
Returns a reference to a procedure to which an event can be bound.
Set object.eventname = GetRef(procname)
parameter
object
Required. The name of the object associated with the event.
eventname
Required. The name of the event to be bound to the function.
procname
Required. This string contains the name of the Sub or Function procedure that is associated with the event.
illustrate
The GetRef function can be used to associate a VBScript procedure (Function or Sub) with any event available in a DHTML (dynamic HTML) page. The DHTML object model provides information about various available events for different objects.
In other scripting and programming languages, the functionality provided by GetRef is known as a function pointer, that is, it points to the address of a procedure to be executed when a specified event occurs.
The following example illustrates the use of the GetRef function:
Copy the code code as follows:
<SCRIPT LANGUAGE=VBScript>
Function GetRefTest()
Dim Splash
Splash = GetRefTest Version 1.0 & vbCrLf
Splash = Splash & Chr(169) & YourCompany 1999
MsgBox Splash
End Function
Set Window.Onload = GetRef(GetRefTest)
</SCRIPT>
It probably means that it is used for binding events. Since I don't know much about events in ASP, I won't discuss too much about the relationship between GetRef and events here. Here we only study the use of GetRef in WEB development applications.
In PHP, PHP does not support pointers, so the function pointer technology cannot be used. PHP supports so-called function variables, which can assign functions to a variable, and their functions are similar to function pointers.
Simpler example:
You can do this in PHP
Copy the code code as follows:
<?php
$funcname = cutstr;
echo $funcname();
function cutstr() {
return function;
}
?>
With GetRef, ASP can also implement functions similar to the above:
<%
dim funcname
funcname = cutstr
response.Write(getref(funcname))
function cutstr()
cutstr = function
end function
%>
Practical applications:
When doing background management projects, such as adding, deleting, modifying news, and other operations are processed on the same page. Generally, we will insert a hidden field action in the form, or the URL parameter action, and determine which step of the operation is based on the value of the action. . Usually written like this:
select case request.querystring(action)
case add: 'The code segment to be operated....
case modify : 'The code segment to be operated....
end select
Or use if else
Using function variables can make the code more concise and clear. Attached is a complete example, which is a commonly used operation in the background. Modify, delete, add.
You can use ?action=××× to test the results
Copy the code code as follows:
<%
option explicit
dim array_action
'Legal module name
array_action = array(list,add,add_form,modify,modify_form,del)
'Output
response.write(getref(action))
'Check whether the parameters are legal
function action()
dim str : str = request.querystring(action)
action = array_action(0)
if arr_in(array_action,str) then action = str
end function
'Operation part----------------------
function list()
list = display list information
end function
function add()
add = perform add operation
end function
function add_form()
add_form = show add form
end function
function modify()
modify = perform modification operation
end function
'--------------------------------
'Check whether it exists in the array
function arr_in(a,v)
arr_in = false
dim i
if isarray(a) then
for each i in a
if i = v then : arr_in = true : exit for : end if
next
end if
end function
%>
Many posts discuss ASP, PHP, and .NET. Which one is better? Personally, I think it is not a language problem, but a thinking problem. Once you understand the functional principles of the website, it will be the same no matter what language you use to write it. Is it efficient? Is the code concise and clear? Then it depends on your own level. Everyone is welcome to discuss!