C ++ programmers learn the three secrets of JavaScript

Source: Internet
Author: User

To be honest, JavaScript is not complicated or even simple. Many Web developers who do not have C, C ++, and other programming basics can quickly master them, but those who have these experiences are not that easy. Some concepts and ways of thinking formed in C and C ++ are not only useless in Javascript development, but misleading. Several problems have indeed brought me trouble.

1. Type

In C and C ++, data has types, including basic data types and user-defined types, such as classes and struct. If you think the same is true for Javascript, you will be miserable. Looking at the Javascript data structure from the perspective of C ++ is messy. Javascript variables have almost no type. A variable can be assigned to any value and can be added with a field (or field or member) at any time ). This is completely different from the C ++ class and has no control. You can change the data structure of a variable anytime, anywhere. This is the JavaScript Object Model (JSON ). Pile up a pile of data.

So what is the class in JavaScript? It is actually a function that generates a pile of data. Because it is generated using the same function, this pile of data also has many commonalities, which is the class.

Ii. Functions

In C and C ++, functions and data are very different, but in Javascript, functions are actually code data. Functions can be assigned as variables. If you are interested, you can print functions as strings. Similarly, a function can be inserted into any data structure and pulled out with parentheses to call it as needed. However, this function itself has nothing to do with the data structure containing it. If you understand it like a member function of the C ++ class, it is totally wrong. It is an independent guy and cannot access other members of the parent data structure at will.

3. This

This is also available in Javascript, but this means no one else can get it. When using JSON to define data, C ++ programmers usually think that this is the data structure containing the function. Sometimes it is true, but not always. Javascript can use any object as this to call a function, such as the call method. After the function is assigned to another position, this will not know who it is.

In fact, there are still many concepts that need to be understood again. The Thinking of C ++ programmers is too complicated. You need to simplify it and take a closer look at the role of JSON in Javascript, which may be useful.

Author: Su Lin

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.