Two big ideas of programming

POP: Process-oriented Programming

Process oriented is to analyze the steps needed to solve the problem, and then use the functions to achieve these steps step by step, when using the time and then one by one call can be.

Process-oriented advantages and disadvantages:

  • Advantages: the performance is higher than object-oriented, suitable for hardware is closely related to things, such as single-chip microcomputer on the use of process-oriented programming.
  • Disadvantages: no object-oriented easy to maintain, easy to reuse, easy to expand

OOP: Object Oriented Programming

Object orientation is to decompose the transaction into objects, and then divide and cooperate among objects.

Find the objects and write down their functions:

  1. The elephant object

    in

  2. The refrigerator object

    Open the

    Shut down

  3. Use elephant and refrigerator functions

    Object orientation divides problems by object functionality, not steps.

In the idea of object-oriented program development, every object is a function center, with a clear division of labor. Object-oriented programming has the advantages of flexibility, reusable code, easy maintenance and development, and is more suitable for large-scale software projects with multi-party cooperation.

Object-oriented features:

  • encapsulation
  • inheritance
  • polymorphism

Advantages and disadvantages of object orientation:

  • Advantages: easy to maintain, easy to reuse, easy to expand, because of the characteristics of object-oriented encapsulation, inheritance, polymorphism, can design a low coupling system, make the system more flexible, more easy to maintain
  • Disadvantages: Lower performance than process-oriented

Classes and objects in ES6

object

In JavaScript, an object is an unordered collection of related properties and methods, and everything is an object, such as strings, numbers, arrays, functions, and so on. Objects are made up of properties and methods: Properties: the characteristics of a thing, represented by properties in the object (common noun) methods: the behavior of a thing, represented by methods in the object (common verb)

class

The new concept of classes in ES6 allows you to declare a class using the class keyword and then instantiate objects from that class.

A class abstracts the common part of an object. It refers generally to a class.

Object refers to a particular object and instantiates a concrete object through a class

Characteristics of object-oriented thinking:

  • Extract (abstract) properties and behaviors shared by objects and organize (encapsulate) them into a class (template)
  • Instantiate the class and get the object of the class

Create classes and objects

Grammar:

class name{
    constructor(parameter1The parameter2..){
        thisAttributes.1= parameter1;
        thisAttributes.2= parameter2; . } method1() {... }}varobject1 = newThe name (argument1That argument2...) ;console.log (object1.attribute1);  / / arguments 1
Copy the code
class Beautiful {
  constructor(uName, age) {
    this.uName = uName;
    this.age = age;
  }
    sayHi(){
        console.log("Hi,I'm "+this.uName);
    }
    sing(song){
        console.log(song); }}var jenny = new Beautiful("Jenny".18);
var tom = new Beautiful('tom'.18);

jenny.sayHi();  //Hi,I'm Jenny
jenny.sing('freezing rain');/ / freezing rain
tom.sayHi();//Hi,I'm Tom
Copy the code
  • The constructor() method is the class’s constructor(default method), used to pass arguments, return an instance object, and is automatically called when an instance of the object is generated using the new command.

  • If we do not write this function, the class will automatically generate this function

  • Generate instance new cannot be omitted

  • When creating a class, do not use parentheses after the class name. When generating an instance, do not use parentheses after the class name

(1) All functions in our class do not need to write function

(2) There is no need to add commas between multiple function methods

(3)