How to use JavaScript dynamic call function (two ways) _javascript skills

Source: Internet
Author: User
Recently users, more and more emphasis on UI interface interaction, gradually originally for a little dragon is only supporting the JavaScript (hereinafter referred to as JS) into the protagonist, accounted for a large piece of the use.

Like the recently popular ASP.net MVC, it's directly included in JQuery.

MVC used for a while, a little dragon even feel the Controller in MVC is basically JS in control, so how to work independently of the UI, cut to the art use, feeling is also a big question.
It seems that behind the art, will only draw, pull the picture is not, must be JS, Flash and so on the front control language, will be popular.

Here, a little dragon introduction, how to use JavaScript dynamic call function, this time need to introduce the premise, so that readers can understand, the following technology can be used where.
Generally used to dynamic call, basically are back-end dynamically generated JS code, in the front of the JS frame, to call use.

will need to do this, the dynamic settings of the UI of the field, style, information, and so on, another possibility, is to retain the expansion of the JS framework, so that the latter developers, according to the characteristics of each program, in addition to write code to expand the original JS frame function.

Here is a brief introduction, dynamic Call function is now known to a little dragon should have the following two ways
Copy Code code as follows:

function Myalert (value) {
document.write ("Myalert-" + Value + "<br>");
}
$ (function () {
Eval ("Myalert") ("Test");
window["Myalert"] ("test");
}

Generally speaking, it is more formal to use the Window object to query whether a function exists, using eval elasticity too much is risky.

and directly according to the above, there is a big risk, a call function does not exist, the entire screen will be wrong, so in the use of the need to add a judgment, from this point eval can not be used, because the use of Eval is to produce the function of this object, and window Only query has object, so when function does not exist, Eval will directly error, so readers should be able to understand the difference.
The example program is as follows
Copy Code code as follows:

function Myalert (value) {
document.write ("Myalert-" + Value + "<br>");
}
$ (function () {
var fnname = "Myalert";
var fn = Window[fnname];
if (typeof fn = = "function") {
fn.apply (window, ["Window-test"]);
}
}

Finally, I came up with another way, as follows
Copy Code code as follows:

function Myalert (value) {
document.write ("Myalert-" + Value + "<br>");
}
$ (function () {
if (typeof (Myalert) = = ' function ') {
Myalert ("Typeof-test");
}
}

Although this way will sacrifice some elasticity, instead, it can be used in this way to limit the naming principles of subsequent developers, such as Init () Load () in the framework, etc., which can be used in such a way to fix the usage, not to diverge, and to facilitate subsequent maintenance costs.
Related Article

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.