JS in the Call_user_func package

Source: Internet
Author: User

PHP Common Call_user_func method, in JS sometimes used, such as you want to according to a dynamic variable to execute the method.

There have been similar problems have not been solved, and now do not quite remember the specific case. Today, I stumbled across a similar article and learned it. The code is as follows:

<script>function Call_user_func (CB, params) {func = window[cb];func.apply (CB, params);} var funcs = [' test1 ', ' test2 '];for (var i=0;i<funcs.length;i++) {Call_user_func (funcs[i], ["DDD", "CC"]);} function Test1 (A, b) {Console.log (A + B + ' is a good gay ');} function Test2 (A, b) {Console.log (a+b+ "is SB");} </script>



The call () and apply () methods in JS are methods that invoke an object and point the pointer to another object
<script>function test3 () {this.name = "ddd"; this.showname = function () {alert (this.name);}} function test4 () {this.name = "CCC";} var t4 = new test4 (), var t3 = new Test3 (); T3.showName.call (T4);</script>



JS in the Call_user_func package

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.