Jquery internal domain access and its method reputation issues

Source: Internet
Author: User
First, understand one thing. If you define a JS function and it cannot be used, there are only two possibilities. First, you encountered an error in introducing the JSP page and introduced the sequence. Second, there is a problem with the scope. You also need to know when the JS you write will be loaded in. [Java... SyntaxHighlighter. all ();

First, understand one thing. If you define a JS function and it cannot be used, there are only two possibilities.
First, you encountered an error in introducing the JSP page and introduced the sequence.
Second, there is a problem with the scope.
 
You also need to know when the JS you write will be loaded in.
[Javascript] view plaincopy
Script
 
Function testAli1 () {alert ("testAli1 ");}
[Javascript] view plaincopy
Var chen = {};
$ (Document). ready (function (){
 
Function testAli2 () {alert ("testAli2 ");}
 
Chen. testAli3 = function () {alert ("testAli3 ");};
 
});
 
;/Script>

The testAli1 () method is loaded in the document loading process. That is to say, the document is executed from top to bottom and the execution time is automatically loaded in.
If it is written in the ready method, it will load these items after all the documents are loaded.
Therefore, writing in is more secure.
However, it will certainly cause new problems. The testAli1 () method is a global method, and you can call it wherever you are.
TestAli2 () is an absolute internal method. You can only call it in ready. It is impossible to call it elsewhere.
If you want to call the above method anywhere, you need to add something like this namespace, and add the testAli3 () method below to call it.
And the calling rule is chen. testAli3 (). If you feel this way, you can write window. testAli3 () directly in it, which is equivalent to a global one.
Example:
(1) Test 1
(2) Test 2
(3) Test 3
(1) normal call (2) Error testAli2 not defined (3) normal call
 
Therefore, the issue that cannot be executed in JS is that you have written an error in the loading sequence. This can be a 1.1-point test, which is very important.
In addition, you can call the methods in the internal scope.
 
Appendix:
When using Jquery, you cannot avoid using the ready function. In fact, this ready function is not only used to load the corresponding js script after the file is loaded, to ensure that some elements already exist in HTML documents before js scripts are used (to avoid some errors ). In fact, the ready function also has an internal field which is invisible to the outside. However, if you want to define a function in the ready scope, it can also be called externally. You just need to set it like this:
Window. funname = function () {// do something };
 

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.