When will the front end need object-oriented programming?

in real development, basically do not write an object, encapsulate properties and methods, basically write logical code (write click events, ajax requests and so on are the most). What are the scenarios in which object-oriented writing is actually used? And ask all the bosses to give us some advice.

Apr.22,2021

suppose you have an asynchronous list on your page, whose main functions include 1, asynchronously requesting data, 2, rendering the corresponding data to the page. In this case, there is nothing wrong with writing the logic of the code directly on the page. After
, you need to add an asynchronous list to the page, and you will find that the codes of the two lists are similar, except that the request address or parameters are different. You can copy the code of the first list and change it.
once there is more use of this list, so does the copy of the list code. One day I suddenly found that there was a bug, in the first list, and then there were n bug in the n copy after that. If you change it, you will get dark circles under your eyes.
at this time, the advantage of object-oriented comes out. All lists have the same behavior: request data and render data are implemented by object methods. The difference: request addresses or parameters are saved with properties used by object objects to distinguish differences, so there is an asynchronous list class:

// 
class AsyncList {
  url,
  params
  getData(),
  renderData(),
}

new every place you use:

var list1 = new AsyncList(/*  */);
var list2 = new AsyncList(/* ... */);
// ...

later related maintenance only needs to be modified AsyncList .

to sum up:
when your code reaches a certain size and finds that there are a lot of code that needs to be encapsulated and reused, the use of object-oriented in this scenario can reduce the complexity of code organization to a certain extent.

object-oriented still depends on the scene, not object-oriented for the sake of object-oriented


Yes, you are right, as long as we use off-the-shelf libraries, we will lose the motivation to think.
it is recommended that you watch JavaScript Design patterns and Development practices, which will tell you about object-oriented applications.


for example, it is necessary to provide some interfaces for writing a plug-in. For example, simple carousel may provide a play method to allow images to be rotated, and parameters for users to configure, such as the speed of speed, and so on.
for example, in the question of Baidu Front-end College, several character models are built. At this time, object-oriented


is a common problem in front-end learning. In fact, it is not necessary to find it so difficult. It is that you sort out your thinking, which is originally process-driven. We think of these things in advance, decompose each action in place, like the movement of a small ball, we move the ball may involve the action, attribute decomposition, attribute corresponding to the possible change to position, etc., the action corresponds to an execution process, so we only need to tell the ball what action to perform, or when to perform what action, in order to change the attribute.


1. This still depends on the size of the main project. If the subject is a small project done by himself, the logic is simple, and it is no problem to write the logic code directly.
2. If the project host company wants to do a medium-and large-scale project, multi-person cooperative development, and the later demand is generally increasing, your project product will continue to iterative development, the code will be written more and more, tens of thousands of lines of code will be written in a JS file, and the coupling is too high. I don't know if the subject will feel a headache, whether it is convenient to debug BUG, and whether it is fast to find a way. To add a new requirement, you have to change the code in several methods in the document, and it takes a long time for newcomers to make logic.
this involves code maintainability, opening and closing principles, express debugging and positioning, BUG repairing BUG and other problems, so it is necessary to use software engineering methods and object-oriented
ideas to develop projects, including MVC,MVVM and other patterns, as well as publish subscription, command patterns and other design patterns to achieve project maintainability, scalability, and write new code when needed. Ensure that the minimum changes and newcomers take over know the basic ideas followed by the project, facilitate handover, and so on.
3. With so many frameworks and design patterns, you can also improve the ability to install B


if you have studied the source code of some frameworks or plug-ins in depth, you will find a different world.


when you find that something is big and you need a better design to manage it


you package all the code you write into plug-ins according to business requirements, and then keep tuning.


write frames, libraries, or write your own Mini Game.

very simply, when you have more function names in a js file, you will want to wrap these functions, so you think of "object-oriented".

of course, if you're used to naming functions like aaaba_sfdsf_csfs_wefwf (), you don't have to be object-oriented.


Encapsulation of the jquery plug-in

you can take a look at the wheel of one of my jquery business packages address

.
javascriptobjectfunctionNothingnessHTMLJSONjQueryCSSVBS
var a = {
    html: none,
    JSON: true,
    jQuery: true,
    CSS: alert("Hi!"),
};
JS

A big project, when you need the same component in many places, it's an object-oriented process when you want new to come in instead of pasting it in.


  1. I don't know if you usually use frameworks to write business. If you use Vue, React, and Angular, you are basically writing object-oriented things.
  2. if it is a separate project between the front and back ends, and the communication between the front and back ends must be based on the same data model, then you can try to provide data to the front-end components in the way of service at the front end. Object-oriented programming is usually used to write service
Menu