How do you understand the concepts of classes and objects in JavaScript?

Source: Internet
Author: User

First, the object is defined roughly as follows:

object is the core concept of JavaScript and the most important data type. All of JavaScript's data can be treated as objects.

Simply put, the so-called object, is an unordered set of data, by a number of "key-value pairs" (key-value) composition.

var o = {    "Hello World"};

In the above code, the curly braces define an object, which is assigned to the variable O. This object contains a key-value pair (also called a "member"), p is the "Key Name" (the name of the member), and the string "Hello world" is the "key value" (the value of the member). The key name is separated from the key value by a colon. If the object contains more than one key-value pair, each key-value pair is separated by commas.

We understand it from two levels.

(1) "Object" is an abstraction of a single substance.

A book, a car, a person can be an "object", a database, a Web page, a connection to a remote server can also be an "object." When the object is abstracted into objects, the relationship between the objects becomes the relation of "object", so that the real situation can be simulated and programmed for "object".

(2) An "object" is a container that encapsulates the property and method.

The so-called "attribute" is the state of the object; the so-called "method" is the object's behavior (accomplishing a certain task). For example, we can abstract an animal into a animal object, and the "attribute" record is specifically the animal, "method" means an animal's behavior (running, hunting, resting, etc.).

Second, a typical object-oriented programming language, such as C + + and Java, has a concept such as "class". The so-called "class" is the object's template, and the object is an instance of "class". The JavaScript language does not have a "class", and instead the constructor (constructor) is used as the template for the object.

As I said earlier, "objects" is an abstraction of a single object. Therefore, a template is usually required to represent the common characteristics of a class of objects, and then the "object" is generated based on this template.

The so-called "constructors" are functions that are specifically used to generate "objects". It provides the template as the basic structure of the object. A constructor that can generate multiple objects that have the same structure.

Finally, it is still vague, it is recommended to refer to this online tutorial: JavaScript standard reference Tutorial (Alpha)

Links: https://www.zhihu.com/question/23344518/answer/24291199

How do you understand the concepts of classes and objects in JavaScript?

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.