Why is JavaScript an object-based language?

Source: Internet
Author: User

"Javascript is not an object-oriented language"

No one opposed this sentence.

So what is object-oriented?

To put it simply, "Object-Oriented is to abstract things in reality". "The word" Object-Oriented "may make it more appropriate ".

Definition 1: The object-oriented method is a method to construct, test, and reconstruct software by using the concepts of objects, classes, encapsulation, inheritance, polymorphism, and messages.

Definition 2: The object-oriented method is based on the theory of cognition. It uses objects to understand and analyze the problem space, and designs and develops a software system (solution space) composed of objects. Because the problem space and the solution space are composed of objects, this can eliminate the problems caused by the inconsistency between the problem space and the solution space structure. In short, object-oriented is the process of thing-oriented analysis, and the process of understanding the objective world.

Is there an Object Concept in JavaScript?

Yes. Check the script to make it correct:

VaR OBJ =   New Object ();
OBJ. Width =   12 ;
Alert (obj. width );

However, JavaScript does not have the concept of inheritance, and thus does not have the concept of polymorphism. In fact, each JavaScript Object is an instance of the same base class.

Many people do not distinguish between "Object-Oriented" and "Object-based. The three characteristics of object-oriented (encapsulation, inheritance, and polymorphism) are indispensable. objects are usually used based on objects, but existing Object Templates cannot be used to generate new object types, then, a new object is generated. That is to say, "Object-based" does not have the inheritance feature, and "polymorphism" is a subclass object instance of the parent class, without the concept of inheritance, we cannot talk about "polymorphism". Without the inheritance and polymorphism features, JavaScript is just an object-based language.

Note:
Today, I am reading jquery. js. We can see something related to prototype. js and talk about JavaScript inheritance. Maybe I was wrong at the beginning and hoped that I would not mislead others. The content is as follows:

Class inheritance is actually supported by JavaScript, but prototype provides another method.
Based on the implementation supported by javascript:

VaR Student = Class. Create ();
Student. Prototype = New Person ();

In this way, student is inherited to person.
This can be achieved when prototype is used:

VaR Student = Class. Create ();
Object. Extend (student. prototype, person. Prototype );

You can use this method to add a subclass.

Student. Prototype. Study = Function () {} ;

Or

Object. Extend (student. prototype, {
Study:Function(){}
} );

 

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.