Less simple JavaScript concepts that should be thoroughly understood (1)

Source: Internet
Author: User

The purpose of this article is to allow more programmers to deeply understand some concepts of JavaScript. In fact, we can see a lot of similar content on the Internet, so now it's time to move closer to a deeper understanding.

BKJIA: uncover the true face of JavaScript closures

I. function

From the very beginning, when I got started with JavaScript, I felt flexible. Everyone's writing methods were different. For example, a function had N writing methods. For example: functionshowMsg () {}, varshowMsg = function () {}, showMsg = function (){}. There seems to be no difference. They are all the same. Is it true? Let's take a look at the example below:

 
 
  1. // Function Definition: naming function declaration), anonymous function reference)
  2. // Declarative, defining code to be parsed before function Execution Code
  3. Functiont1 (){
  4. Dwn ("t1 ");
  5. }
  6. T1 ();
  7. Functiont1 (){
  8. Dwn ("newt1 ");
  9. }
  10. T1 ();
  11. // Reference type for dynamic parsing during function running
  12. Vart1 = function (){
  13. Dwn ("newnewt1 ");
  14. }
  15. T1 ();
  16. Vart1 = function (){
  17. Dwn ("newnewnewt1 ");
  18. }
  19. T1 ();
  20. // The output is newt1, newt1, newnewt1, and newnewnewt1.

I may think that I should output t1, newt1, newnewt1, and newnewnewt1, but the result is not like this. I should understand this sentence: declarative, defining code before the function Execution Code is parsed. If we take a further step, we should say that it is a scope chain problem. In fact, the first two methods are equivalent to window. t1 is a public attribute of window. It is assigned twice and the last value is the final value. The next two methods can be understood as t1 is a variable, and the result after var of the fourth method is removed remains unchanged.

However, when the fourth method is changed to a declaration such as functiont1 () {}, the result is changed to newnewnewt1, newnewt1, newnewt1, and newnewt1. According to my understanding, the first two can better understand why this is the answer, and the third one can also understand it. However, the last output makes me more difficult. I hope some experts can answer this question. In addition, anonymous functions include (function (){...}) () in this way, the last bracket is used for parameter input and vart1 = newfunction (){..} in fact, t1 is already an object.

 
 
  1. Vart2 = newfunction ()
  2. {
  3. Vartemp = 100; // Private member
  4. This. temp = 200; // public member. The two concepts will be described later in the third part.
  5. Returntemp + this. temp;
  6. }
  7.  
  8. Alert (typeof (t2); // object
  9. Alert (t2.constructor (); // 300
  10. In addition, a built-in function object is used to construct a function, for example:
  11. Vart3 = newFunction ('vartemp = 100; this. temp = 200; returntemp + this. temp; '); // The result of adding this position without adding new is the same, WHY
  12. Alert (typeof (t3); // function
  13. Alert (t3 (); // 300

2. Create an object

First, let's take a look at Object-Oriented Programming Object-OrientedProgramming, OOP). With OOP technology, many code modules are often used. Each module provides specific functions, and each module is isolated, it is even completely independent from other modules. This modular programming method provides great diversity and greatly increases the chance of code reuse. This problem can be further illustrated by examples, assuming that a high-performance application on the computer is a first-class racing car. Using traditional programming skills, this racing car is a unit.

To improve the car, you must replace the entire unit, send it back to the manufacturer, ask the car experts to upgrade it, or buy a new car. If you use OOP technology, you only need to purchase a new engine from the manufacturer and replace it with the instructions, instead of using a steel saw to cut the car body. However, most of the arguments are that JavaScript is not a direct object-oriented language, but through simulation, it can do a lot of things that can be done by object-oriented languages, such as inheritance, polymorphism, encapsulation, javaScript can be used.

 
 
  1. // The following three methods for constructing objects:
  2. // NewObject: instantiate an Object
  3. Vara = newObject ();
  4. A. x = 1, a. y = 2;
  5. // Object Direct Volume
  6. Varb = {x: 1, y: 2 };
  7. // Define the type
  8. FunctionPoint (x, y) {// similar to the class in C #
  9. This. x = x;
  10. This. y = y;
  11. }
  12. Varp = newPoint (1, 2); // instantiate the class

The first method is implemented by constructing a basic object to add attributes directly. The second method is similar to the first one, which can be seen as a quick representation of the first method. In the third method, you can create multiple objects of the same type based on the "class.


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.