Use gb2312 encoding and decoding in JavaScript

Source: Internet
Author: User

Through this article, you can understand:

Anonymous Functions

Closure generation

JavaScript implements private and public Access Permissions
Document. cookie operations

Javascript does not have private and public access permission setting keywords. However, you can use certain techniques to simulate the same results.
First, let's look at the following line of code:

Var I = (1, 2, 3, 4, 5 );
The final result of variable I is 5.
This is the result of the comma operator. That is to say, the last value is returned. Parentheses change the priority of this line of code. Otherwise, var I = 1, 2, 3, 4, 5; an error is returned with a missing identifier.

Var I = (1, 2, 3, 4, function () {return 5*5 ;});
The final result of variable I is a function and Result 25 is returned.
This is the flexibility of Javascript. It can assign values of any type without being declared in advance. Now we can make the following calls:

I ();
Alert (I ());

To obtain a method call that returns 25.

The variable I is used to obtain the reference of the function, that is, the reference to the last result returned after the parentheses on the right of the equal sign are still present, although we cannot display the call, but it does exist. What if it is not called by referencing variables?

(1, 2, 3, 4, function () {alert (5*5 );})()
After the above code is executed, a message box is displayed, showing 25.
For ease of display, I changed the function in the previous example to a pop-up message box.
Two pairs of parentheses (); the first pair indicates that a result is returned. If the result is a function, the second pair of parentheses is called.
That is to say, an anonymous function is referenced through a pair of parentheses in order to reference it below. This is the call to an anonymous function.
For more use of anonymous functions, refer to reference connections at the end of the document.

The reason for the closure is that the scope is different. The sub-scope references the variables of the parent scope and returns the sub-scope. The parent scope should be destroyed after execution, it is only because the sub-scope always exists and the reference of the parent scope is always held.
Let's look at the following code:

1 function parent (){
2 var a = 1;
3 function child (){
4 var B = 2;
5 alert ();
6 alert (B );
7}
8}
The parent function parent contains a child sub-function, which has a reference to the variable in the parent function (output value ).
Let's let the parent function return the declared sub-function after execution.

1 function parent (){
2 var a = 1;
3 function child (){
4 var B = 2;
5 alert ();
6 alert (B );
7}
8 return child;
9}
10 var t = parent ();
11 t ();

In row 10, we executed the parent function and returned the child function declared in the function. At this time, variable t holds the reference of the returned object (an executable function, we called it in 11 lines of code. the results output 1 and 2 respectively.
Note: Output 2 is because we declare a variable in the sub-function body, while output 1 does not have the corresponding definition variable a in the function body, instead, a reference is made to the variables in the parent function, that is, the variables in the parent scope are referenced.
At this time, the output can be completed, that is, variable a still exists. however, we cannot directly reference it (such as parent. a) because the function has been executed and no reference is available, we can only access it through the reference of the returned sub-function.
What if I declare other variables in the parent function? The results are the same. The subfunction can be accessed. If the subfunction does not return a reference, we cannot access the subfunction from the External. This forms a closure.

What can closures do? What should you do if you have a variable that you do not want to allow external modifications at will? Use the closure.

1 myObj = {}; // declare a global variable, which is the property of a window object (window. myObj)
2 (function (){
3 var I = 4;
4 myObj = {// reference the global variable and assign a value to it
5 getI: function () {// get method, a function
6 return I;
7 },
8 setI: function (val) {// set method, limit value setting
9 if (val> 100 ){
10 alert ("I connt> 100 ");
11 return;
12}
13 I = val;
14}
15}
16}) (); // anonymous function call. Because it is also a function, as a subscope, it is destroyed after execution to avoid code contamination.
17 myObj. setI (5); // succeeded
18 myObj. setI (101); // failed
19 alert (myObj. getI ());
20 alert (myObj. I); // error, no such attribute

At this point, we have implemented the public access permission and private access permission (this is what you want to give you, not what you don't want to give you)

 

On the page, we usually use document. when the cookie attribute is used for access, a new Cookie is created when a new value is assigned to it. A Cookie usually has five attributes: value (stored value), date (UTC time, indicates the expiration time, domain (domain, Cookie owner), Path (subdirectory ).

In normal development, if you only use document. cookie used ument. when a cookie is read, all values are returned, excluding the expiration time, domain, and other information. It can only be set again.

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.