In the document upgrade scheme, because a new template is used, I need to perform some checks before the original system executes some functions. If it is set up, the original function is executed. Otherwise, my new logic is executed, then, determine whether to execute the original function based on the actual situation.
We know that Javascript Functions include static functions, member functions, and member functions of instantiated objects. The implementation of these functions is differentiated between normal functions and anonymous functions. So when we Hook members, we need to pay attention to these situations.
For example, if you call a Hook with the following function, the following output is expected:
// The global function has no parameters.
Function Method1 (){
Alert ('method1 ');
}
Method1.hook (function ()
{
Alert ('befor Method1 ');
Return true;
},
Function ()
{
Alert ('after Method1 ');
}
);
Method1 ();
/* Output
Befor Method1
Method1
After Method1
*/
Alert ('-----------------');
// Global functions have Parameters
Function Method2 (name ){
Alert ('method2 with '+ name );
}
Method2.hook (function (name)
{
Alert ('befor Method2 with '+ name );
Return true;
},
Function ()
{
Alert ('after Method2 ');
}
);
Method2 ('evlon ');
/* Output
Befor Method2 with evlon
Method2 with evlon
After Method2
*/
Alert ('-----------------');
// Hook the toString Function
String. prototype. toString. hook (String. prototype, function ()
{
Alert ('befor string. tostring ');
Return true;
});
Var s = "return s ";
Alert (s. toString ());
/* Output
Befor string. toString
Return s
*/
Alert ('-----------------');
// Hook the system's existing global function parseInt
ParseInt. hook (function ()
{
Alert ('befor parseint ');
Return true;
});
Alert (parseInt ('5 '));
/* Output
Befor parseInt
5
*/
Alert ('-----------------');
// Hook the join method for all array objects
Array. prototype. join. hook (Array. prototype, function (span)
{
Alert ('befor Array. prototype. join separator' + span );
Return true;
});
Alert ([3, 5, 6]. join (','));
/* Output
Befor Array. prototype. join separator,
3, 5, 6
*/
Alert ('-----------------');
Var list = [3, 5, 6];
// Hook list the join method of the Instance array object
List. join. hook (list, function (span)
{
Alert ('befor list. join separator' + span );
Return true;
});
Alert (list. join (','));
/* Output
Befor list. join separator,
Befor Array. prototype. join separator,
3, 5, 6
*/
Alert ('-----------------');
Var list2 = [3, 5, 6, 7];
// This call will not generate befor list. join separator,
Alert (list2.join (','));
/* Output
Befor Array. prototype. join separator,
3, 5, 6, 7
*/
Alert ('-----------------');
// Custom class
Function People (){
// Member functions with Parameters
This. getName = function (name ){
Alert ('in getName with '+ name );
Return 'Return evlon ';
}
}
Var p = new People ();
Var p2 = new People ();
// Here we only Hook the function call of instance p2
P2.getName. hook (p2, 'getname2 ',
Function (name ){
Alert ('befor getName2 with '+ name );
Return true;
},
Function (){
Alert ('after getname2 ');
}
);
P2.getName. hook (p2,
Function (name ){
Alert ('befor getName with '+ name );
Return true;
},
Function (){
Alert ('after getname ');
}
);
// Because only P2 is hooked, this call is not affected.
Alert (p. getName ('argname '));
/* Output
In getName with argName
Return evlon
*/
Alert ('-----------------');
Alert (p2.getName ('argname '));
/* Output
Befor getName with argName
In getName with argName
After getName
Return evlon
*/
Alert ('-----------------');
Alert (p2.getName2 ('argname2 '));
/* Output
Befor getName2 with argName2
In getName with argName2
After getName2
Return evlon
*/
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page