Home  >  Article  >  Web Front-end  >  JavaScript Learning (2) Summary of JavaScript Common Problems_Basic Knowledge

JavaScript Learning (2) Summary of JavaScript Common Problems_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 17:44:361065browse

1. Methods and variables in JS are case-sensitive

2. There is no special difference between single quotes and double quotes in JS, and both can be used to create strings. But as a general rule, most developers prefer single quotes to double quotes, but the XHTML specification requires that all attribute values ​​must be enclosed in double quotes. Using single quotes for JS and double quotes for XHTML makes it easier and clearer to mix code between the two.
Single quotes can contain double quotes, and similarly, double quotes can contain single quotes.

3. Brackets
First of all, it needs to be explained that brackets in JS contain two semantics, which can be delimiters or expressions.
a. Everyone is very familiar with the delimiter (1 3) * 3 equals 12
b. (function(){})(); The pair of brackets before function are used as delimiters, and the following brackets indicate that this will be executed immediately method.

4. Function calls and references
Because brackets represent execution, so:
var foo=example(); foo represents the return value of the function
var foo1=example; assign function reference Give a value foo1

5. Overloading
JS does not support overloading, so the overloading mentioned here is actually more similar to replacing
JS does not distinguish the number of parameters

6. Scope and closure
Scope refers to the code space that has access rights to a certain attribute or method. For example:

Copy code The code is as follows:

function myFunction(){
var temp ="abc";
}

The above temp cannot be accessed outside the function.
Closure is a concept related to scope. It refers to the property that the inner function can still access its outer function even after the outer function has completed execution and terminated.

Let’s give an example of scope and closure below:
We create the following html page:
Copy Code The code is as follows:





Untitled Document



abc

abc

abc




Running result:
pic
As you can see, this is not What we want.
After modification, the key code is as follows:
Copy code The code is as follows:

< ;script language="javascript" type="text/javascript">
function init(){
for(var i=1;i<=3;i ){
author=document.getElementById ("author" i);
registerListener(author,i);
}
}

function registerListener(author,i){
author.onclick=function(){
alert("author" i);
}
}
window.onload=init;


In this way, we get get the results we want.
This is because every time init is called, an instance of function is generated, and a new i is maintained in each instance.
And the upper level of the anonymous function already has this i, and it has been compared with the one in init. i is different.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn