1. What are the three objects of MVC to do respectively and give pseudocode examples
M-model
Used to encapsulate data related to the application’s business logic and how it is processed
// Add, delete, modify and check data
m = {
data: {},
create() {},
delete() {},
update() {},
get(){}}Copy the code
V-view Indicates the View
The on-screen representation represents the current state of the Model. As the model’s data changes, the view gets the opportunity to refresh itself accordingly.
// Initialize, build HTML skeleton, render
v = {
el: null.html: <template>,
init(container) {
v.el = $(container)
},
render(n){}}Copy the code
C-controller Indicates the Controller
Defines how the user interface responds to user input, acts as an organization between different layers, and is used to control the flow of the application, which handles user behavior and changes to the data model.
c = {
init(container){})},events: {}, / / event
actions: {}, / / operation
autoBindEvents() {} / / binding}}Copy the code
2. What apis does EventBus have? What is it used for
EventBus can be called the Observer design pattern for event publishing/subscription
Basic APIS for EventBus:
- On (Listening event)
- This trigger triggers events.
- Off (Cancel listening)
constructor(){
this._eventBus =$(window)}on(eventName, fn){
return this._eventBus.on(eventName,fn)
}
trigger(eventName,data){
return this._trigger.tiggger(eventName,data)
}
off(eventName, fn){
return this._eventBus.off(eventName,fn)
}
Copy the code
3. Table driver programming is to do (you can check the information)
Table driven programming is described in Code:
Table-driven methods are methods that allow you to look up information in a table without having to use logical statements (if or case) to find it. In fact, any information can be selected by the table. In simple cases, logical statements tend to be simpler and more straightforward. But as logical chains become more complex, tables become more attractive.
The meaning of table driven programming lies in the separation of logic and data. (Similar to event delegation)
It can be simply interpreted as that when faced with too many conditional expressions, table queries can be used instead to avoid spaghetti code.
4. How do I understand modularity
Modularity, based on the principle of minimum knowledge, is a guiding principle of object-oriented programming, which describes a strategy to keep code loosely coupled.
Each unit has only limited knowledge of other units and only knows the units closely related to the current unit;
The fragmentation of a complex program according to a set of rules into different pieces, which can be combined by introduction to weaken the interaction between code blocks.
- Avoid variable contamination and naming conflicts
- Improve code reuse
- Improve maintainability
- Dependency management