Javascript-how to elegantly answer "What is object-oriented "?

Source: Internet
Author: User
I found that many interviewers like to ask this question (I am a student). However, every time I feel that I have not answered this question well. My answer is often that it is designed with the following features: inheritance, encapsulation, polymorphism; from concrete to abstract. Attributes change to variables, and behavior changes to methods. However, the interviewer seems to have found that many interviewers like to ask this question (I am a student). However, each time I feel that I have not answered this question well.
My answer is often

  • Its design features: inheritance, encapsulation, and polymorphism;

  • From concrete to abstract. The attribute is changed to a variable, and the behavior is changed to a method;

However, the interviewer does not seem very satisfied. do you have any more elegant answers?

Reply content:

I found that many interviewers like to ask this question (I am a student). However, every time I feel that I have not answered this question well.
My answer is often

  • Its design features: inheritance, encapsulation, and polymorphism;

  • From concrete to abstract. The attribute is changed to a variable, and the behavior is changed to a method;

However, the interviewer does not seem very satisfied. do you have any more elegant answers?

The first answer isProgramming paradigm.

Then, let's take a detailed description of the three features you mentioned, for example:

  • Inheritance: The subclass can reuse the variables and methods exposed by the parent class;

  • Encapsulation: Shields a series of details. To enable external calls, you only need to know the existence of this method;

  • Polymorphism: methods of the parent class can be implemented in different ways after inherited to the subclass;

Then you can talk about the advantages and disadvantages of object-oriented, so there is no absolute advantage or disadvantage for one thing, and a comparison is needed. So you have to talk about it.Process-oriented programming.

I lost my article.

After reading the lz question, I checked it in the community. there are indeed related answers. My article is a small supplement. if you have any questions, I hope you can point it out.

Object-Oriented Thinking

Then I will use my instance at work to answer the question. some time ago, I would like to make a very simple function,Thumb upsIt is indeed a very simple function, but the thumb-up function is not just one time. Users can give comments to others, like articles, and like questions, in addition, a series of events (notify related users, etc.) may be triggered after thumb ups. At first I thought there were three ways to write

// 1. user likes $ article-> votedBy ($ user); // 2. the user likes $ user-> voteArticle ($ article) for an article; // 3. the user gives a thumbs up and likes $ user-> thumbUp ()-> voteToArticle ($ article); // $ user-> thumbUp ()-> voteToDiscuss ($ discuss );

Finally, I used the third method. how can I implement it? you will know it when you reach out your thumb.

This is really an interesting question, haha. Of course, everyone has a different understanding. after all, the concept of object-oriented is also hyped up.

My understanding includes two aspects:

  • One is object-oriented design;

  • One is object-oriented programming.

The core idea of object-oriented design is modularization, while the core idea of object-oriented programming is interface-oriented. another relative of object-oriented programming is object-based programming or prototype programming (JavaScript is ). Therefore, object-oriented programming generally requires language-level support, just like the encapsulation, inheritance, and polymorphism mentioned above.

When object-oriented concepts are very popular, it is often said that software reuse, inheritance is a direct embodiment of software reuse. However, people later found that they should use more combinations instead of inheritance, and face-to-interface programming will be on fire.

It's a little far away. stop here.

Everything is an object.

Object-oriented

Everything is object

It must be used as an example.
For example, animals-furu animals-people, Lions, Tigers

With polymorphism, for example

Inheritance, for example, furu animals are born...

When implementing a project, you need to abstract the business into code. this abstract process is the programming paradigm, and object-oriented is one of them. Similar to process-oriented, functional programming.

Object-oriented features:Inheritance,Polymorphism,Encapsulation.

Then the interviewer can start to askInheritance,Polymorphism,Encapsulation.

Perfect !~~~

It is better to know what is process-oriented and functional programming.

Self-contained. if he doesn't understand it, you can leave. this interviewer is too good!

Js says: everything is an object, and objects are also objects-no problem

Well, everything is an object.

I remember zhihu's understanding that the most important thing is code reuse.

Encapsulation: encapsulated code blocks can be used directly, which is reused.

Inheritance: use existing attributes, which are reused.

Polymorphism: different details, but must be reused first.

Without discussing profound theoretical things such as program design and paradigm, the most direct advantage and experience for writing code itself is that the code becomes concise with more reuse.

Target audience (commonly known as male/girlfriend), put on an elegant posture, then tell him/her affectionately, look at me ...... This is an elegant ground object.

Package data and related behaviors

Interesting question. I am also a student and want to talk about my own understanding.

Object-oriented has a major feature: it combines data and behavior (operations on data) at the same time,It is more similar to the operating logic of the real world..
Why do you think this is a comparison with traditional ones (process-oriented.

For example, the C language processes data and behaviors in a bit separately and places more emphasis on behavior. It does not have the concept of "object", but a world full of actions (functions. It is mainly for function calls, and results are given from the beginning to the end ...... The most important thing in the world is various actions.

C ++'s "object" is especially like a thing, such as a person. Its world is a world full of objects. The relationship between objects and objects (people and objects, people and people). These relationships or collisions lead to the final result ...... The most important thing in the world is various objects.

Objects themselves carry data and functions. only when the relationships between objects occur can they run in the world. In these two aspects, object-oriented programming is more similar to the real world.

The above "inheritance, encapsulation, polymorphism" and other features can also be used to simulate a better understanding of the real world. Let me give you some examples ......

  • Inheritance: Books, teaching materials, and computer-this is the inheritance relationship of the real world.

  • Encapsulation: Mobile phone-it is encapsulated. when you use it, you do not have to know the circuit logic in it.

  • Polymorphism: Person. Watch (beauty) & Person. Watch (strong light) -- The parameter type is different and the execution is different.

The benefits of object-oriented are easy to understand. One is to improve comprehensibility, because objects are presented according to the laws of human beings who know the world and conform to common ways of thinking. The second is easier to develop and maintain, to a large extent, system optimization is equivalent to a new understanding of object data and functions, and modifying some things based on the original.

Where is it wrong? please criticize and correct it ......!

I don't rule out that some interviewers are very watery. this is a big fucking topic. I can't talk about it for a few days.

Easy to know and write more code

Everything is an object.

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.