Common js Problems

Source: Internet
Author: User
Tags id3

Common js Problems
Case Sensitive
Single quotation marks, double quotation marks
-Single quotes are used in js.
-The attribute value of html must be enclosed in double quotation marks.
-Single quotation marks can contain double quotation marks, which can also contain single quotation marks.
Brackets
-It can be a separator or expression. As follows:
-(Function (){})()
-The onload function is loaded when the entire page is loaded. (If there are many images, it will be very slow. You can rewrite the window. onload function to execute the function at the same time when loading the text)
Function call and reference
Var foo = functionName ();
Var foo1 = functionName;
The former indicates the return value of the function, and the latter indicates a reference to the function. For example, window. onload = functionName;
Newline
One line in js is the default statement and cannot contain a forced line break. If you want to wrap a line, you can add a backslash at the end of the line or use a string connector +. We recommend that you use the latter.
Semicolon and braces (optional)
The semicolon can be left blank, but we recommend that you add it for code readability.
Heavy Load
Js does not support overloading. If two functions have the same name (not related to parameters), the following functions will overwrite the above functions.
If function alert () {}; appears in the Code, the core function alert is overwritten.
Therefore, do not use the name of the core function when naming a function.
Scope and Closure
Scope: the code space that has access permissions to a property or method.
Closure-an internal function can access the attributes of an external function even after the external function is executed and terminated.
The related code is as follows:

 

====================================

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
<Script language = "JavaScript">
<! --
// (Function () --- in this way, the hid will return null, not an object, and cannot be found.
Function hinit ()
{
For (var I = 1; I <= 3; I ++ ){
Var hid = document. getElementById ("id" + I );
// Differences between calling external instances and closures:
// Saves a unique object for the instance.
RegisterListener (hid, I );
// Error code -- id4is displayed. Cause: I is not found internally when listening to The onclick event, so I can only be found in the external scope. In this case, I = 4;

// Hid. onclick = function (){
// Alert ("id" + I );
//}
}
}

//)();

Function registerListener (hid, I ){
// At this time, I can be found in the internal scope
Hid. onclick = function (){
Alert ("id" + I );
}
}
Window. onload = hinit;
// -->
</SCRIPT>
</HEAD>


<BODY>
<A id = "id1" href = "#"> id1 </a> </br>
<A id = "id2" href = "#"> id2 </a> </br>
<A id = "id3" href = "#"> id3 </a> </br>
</BODY>
</HTML>


From cainiao's development path

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.