Javascript learning experience

Source: Internet
Author: User
It has been two weeks since I started to access Javascript. I have a lot of feelings and share some of them here.
 
Although Javascript is object-oriented, I have already talked about my own ideas in the previous "chaotic" articles. I have started to contradict this language, why does the language claim to be object-oriented? It is very painful to start learning a new thing, especially when some existing concepts or understandings conflict with the east-west to learn? Have the impulse and idea to give up.
 
I am a person who cannot give up easily. Even if I cannot accept it for the moment, I may put it down for a while and start over again. Maybe this is the method I have been using over the years, intermittent persistence, enjoy the process of suffering, every time I want to improve a little bit than the last time until I fall in love with him.
 
I fell in love with this word and said it was a bit too much. The word torture was extracted from the fable "the Eagles are reborn". I like this prediction story very much, fable Stories are often inspiring and powerful. However, this prediction is true, especially in this society. To get what you want, and to learn what you like, you have to pay the price and experience some pain. The hardships of the process will make people remember and be more secure.
 
Do not do it or do not. This is my commitment to myself and I will do my best.
 
I think the anonymous functions, prototype, and closure in javavascript are very important. If you have mastered them, the main content of JavaScript is no longer a problem, and then the content of CSS.
 
The first difference between each discipline, technology, or other disciplines is that you have your own terminology. The proprietary field (many of them are comprehensive) also provides a solution to the problem.
 
For professional terms, you should first study carefully and then use your own words to explain them, the application process is to use its own methods to solve some problems in some of its fields.
 
Today, with the prevalence of the Internet, search engines have played a major role, allowing everyone to quickly obtain knowledge and solve problems, how to quickly obtain and solve problems, this should be a new kind of ability for a person, and I have been thinking about it.
I am always so arrogant. Now let's get down to the truth and talk about the ones I mentioned just now.
Anonymous Functions
 
An anonymous function is a function without a specific name. Let's take a look at it through an example.
 
In JavaScript, we generally declare a function in the following way:
 
1, function Double (x) {return 2 * X;} 2, VAR double = new function ('x', 'Return 2 * X; '); 3, vaR double = function (x) {return 2 * X ;}

 

 




From these three methods, we can see that there is a name to locate the function, and the declaration of the anonymous function is different from this, as shown below:
 
(Function (x, y) {alert (x + y) ;}) (2, 3 );
 
 
 
Anonymous functions can be the most self-executed functions and callback functions (if jquery and other frameworks are used, you can see a lot)

Closure

A closure is a strange concept. In my own words, a function is defined in a function, and the function variable or name is returned (of course, no return can be done, specified through this ), after the return result, the resources occupied by the original function are not released. It can be said that the function is in the active state. The interface is relatively unavailable. See the following example:
Function F1 () {var n = 999;
Function F2 ()
{
Alert (N );
}
Return F2;
}
VaR result = F1 ();
Result (); // 999.

 

 
 
 
From this function, we can see that in F1, another F2 is defined, F2 is returned, and then the Variable N defined in F1 is still accessible after the F2 is returned, this is also a function of the closure. Access the internal parameter. If the variable is changed, access the final value of the variable.

 
Function buildlist (list) {var result = []; for (VAR I = 0; I <list. length; I ++) {var item = 'item' + list [I]; result. push (function () {alert (item + ''+ list [I])});} return result;} function testlist () {var fnlist = buildlist ([1, 2, 3]); For (var j = 0; j <fnlist. length; j ++) {fnlist [J] () ;}} testlist ();
The above is a closure usage. When fnlist [J] () is called, the pop-up item undefined is displayed, Because I has changed to 3 after the return, list [3] does not exist.
Prototype

Prototype is essentially an object, and common instance objects are essentially different. It can contain the shared attributes or methods of all instances of a specific type. Each JavaScript function has the prototype attribute. This attribute references an object, which is a prototype object. The initialization of the prototype object is empty. We can customize any attributes and methods in it. These methods and attributes will be inherited by the object created by the constructor.
The following example intuitively illustrates this.
 
Function user (name, age) {// constructor this. name = Name; // object property this. age = age;} user. prototype. ADDR = 'hubei Wuhan '; // Add the property user to the prototype. prototype. show = function () {// Add method alert (this. name + '|' + this. age) ;}; var user1 = new user ('zxc ', 22); // create an instance var user2 = new user ('cxz', 21); user1.show (); // call the show () method user2.show (); alert (user1.show = user2.show); // return true, indicating that the show method is a shared alert (user1.addr ); // 'hubei Wuhan 'alert (user2.addr); // 'hubei Wuhan'
 through prototype, We can dynamically expand the functions of an object. I don't know if this method is used for many framework functions? 
many of these materials are from the Internet. Thank you for sharing them and share these addresses as follows:
http://bonsaiden.github.com/JavaScript-Garden/
http://www.cnblogs.com/jsk540/archive/2010/10/26/1861630.html
http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html
http://www.cnblogs.com/phpzxh/archive/2009/09/16/1568137.html
http://www.cnblogs.com/chaofan/archive/2009/12/29/1635133.html
http://kb.cnblogs.com/page/144404/
http://blog.csdn.net/helloworl/article/details/7883507

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.