On the object-oriented summary of JS

Source: Internet
Author: User

What is Object-oriented: objects consist of two parts: properties and methods; object-oriented features:
  • 1. Encapsulation: For the same function of code, put in a function, and later use this function, only need to call, no need to rewrite; avoid a lot of redundant code;

    Professional words: Low coupling, cohesion poly;

  • 2. Inheritance: Subclasses inherit the original properties and methods of the parent class;

    Class: ' Object ', ' Function ', ' number ', ' String ', ' Array ', ' RegExp ', ' Date ' .....

  • 3. Polymorphism: overloading and rewriting;

    Overloading: There is no strict overload in JS; however, JS has similar overloaded functions: the same function, the different parameters, to achieve different functions;

    Overriding: Subclasses can override properties and methods of the parent class;

  • 4. Design patterns that will be learned: Singleton mode, Factory mode, constructor mode, prototype mode;

  • 5. Singleton mode: The property and method of describing the same thing are placed under the same namespace, which avoids the problem of variable name conflict.

    Namespaces: The browser opens up a heap of memory, assigning him the name Person1 is the namespace

    The nature of Singleton mode: ordinary object;

  • 6. Modular development: For a large project, the project team will be assigned to different engineers to develop (these developments are synchronized); When all the developers are finished, together, the whole project is completed;

    • 1) Single example is a simple modular development;
    • 2) can realize the mutual invocation between the modules: this. property name;
    • 3) Mutual invocation between modules can be implemented: module name. attribute name;
  • 7. Advantages and disadvantages of the singleton mode:

    • Advantages:
      • 1) Modular development can be achieved
      • 2) Avoid the same property name, and the same variable name of the conflict problem;
    • Disadvantages: The traditional manual operation mode, the development efficiency is low, and causes the massive redundant code;

    Solution: Encapsulation-Factory mode

  • 8. The idea of a factory model:

    • 1. Introduction of a batch of raw materials----1.var obj={}; Var obj=new object (); We create an empty objects ourselves
    • 2. Processing of raw materials---2. Add some private properties and methods to the empty object;
    • 3. Output raw Material---3. Output object return obj;

    The nature of the factory model: encapsulation;

  • 9. Constructor mode:---in order to customize a class, and can create some instances;

    instance instanceof class; = = Returns a Boolean value;

    The difference between a constructor pattern and a factory pattern:
    • 1. At the time of the call

      • constructor new person ();
      • Factory mode person ();
    • 2. The difference in the function body;

      • Constructor: The system automatically creates an object, and when we finish processing the object, the system automatically outputs the object;
      • Factory mode: Manually create objects, and then after we have finished processing the object, manually output the object;

      Disadvantage: For the same function, but not equal;

      Solution: Prototype prototype, put the same function code, put in a common interval;

  • 10. About Constructors:

    • 1. The constructors are all private properties and methods;
    • 2. Is the case and the class in dealing with;
    • 3. When creating an instance, the parentheses can be omitted if no arguments are required;
    • 4. Constructor this, which always points to the current instance;
    • 5. In the constructor, the instance is only related to this.xxx and has no relation with the variable;
    • 6. In the constructor, the system returns an object for us by default, if we return it manually:
      • 1) Return basic data type, without any effect; the instance also has his attributes and methods;
      • 2) Return reference data type, will affect the object returned by the system, the instance does not have his previous properties and methods, it is not recommended to manually return objects;
  • 11. Prototype Mode: Prototype prototype

    • 1 when we declare a function (constructor, Class), we are born with a prototype property.
      • 1 and the value of this prototype is also an object type
      • 2 This object type value also has an innate property called constructor and the value of this property is the function (constructor, class) itself
    • 2 An instance of this class will also have a naturally-owned property called __proto__, and the value of this property is also an object type whose value is the prototype of the class to which the instance belongs.
    • 3 Each reference type has a naturally inherent property called __proto__, so the value of our prototype also has a property that is inherently from a __proto__. And the value of this property is also an object type, until our base class object
    • 4 properties and methods that are added through the class's prototype are public, and each instance comes with a
    • 51 instances of the method at run time, if this method is private, then directly with, if not private, then through the __proto__ to the class of the prototype to find, if not yet through the prototype of the __PROTO__ The object of the base class has been checked. The fruit has not been error-free, if any, it is used directly. We call this mechanism of finding through __proto__ a prototype chain.
  • 12. Basic knowledge of prototype mode: the most serious

    • 1) Each function data type (class, Ordinary function) is born with an attribute, called prototype (prototype), it is an object;
    • 2) prototype This prototype, born with an attribute, called constructor, points to the current class; constructor: Class;
    • 3) Each object (instance, ordinary object, prototype) is born with an attribute, called __proto__, that points to the prototype of the class to which the current instance belongs;
  • 13.object.prototype: Public properties and methods of all-in-place

    • hasOwnProperty: Determines whether the Attr property is a private property on this object;
    • Whether the isprototypeof:obj1 is on the OBJ2 prototype chain;
    • propertyIsEnumerable: Whether an enumerable property is available;
  • 14

    • Each class is a function data type;
    • object is the base class for the data types of the objects;
  • 15

    • Constructor mode: instance and class;
    • Prototype patterns: examples, classes, prototypes;
    • In the constructor: private properties and methods;
    • Prototype: Public properties and methods;
  • 16. Prototype chain lookup mechanism: For example, to find the F1.x==> object. Property name

    • 1) find it on its own private property, if found then this property is a private property;
    • 2) If not found, through the __proto__ to the class of the prototype to find, because the prototype is placed on the public properties and methods, so, if found, this property is public;
    • 3) If not found, through a layer of __proto__ to find, eventually found on the base class Object.prototye, if not yet, undefined!
  • 17. Rewrite: Subclasses through the __proto__ to modify the parent class's properties and methods, this is the subclass of the parent class overrides, since the subclass can override the parent class, the system in order to prevent subclasses through __proto__ to change the system built-in properties and methods, so in IE browser, prohibit us to use __ proto__;

    • 18. Inheritance: Subclasses can inherit the original properties and methods of the parent class, but when the subclass adds private properties and methods, it does not affect the parent class;
    • 1) prototype chain inheritance: The parent class's Private + public properties and methods are all public properties of the subclass; Core: not to clone the parent private + public properties of the same to the subclass of the public bar; he is the prototype chain between the __proto__ and the sub-class, When an instance of a subclass needs to use the properties and methods of the parent class, it can be used at the __proto__ level.
    • 2) Call Inheritance: The private properties and methods of the parent class are given to the subclass private properties and methods, the core idea: the equivalent of the parent class private properties and methods to clone a copy of the same to the subclass of the private property;
    • 3) Impersonate the object inherits: The parent class public's + private property all takes the child class private property;
    • 4) Hybrid Inheritance 1:call inheritance + prototype chain inheritance call Inheritance: The parent class private as their own private, prototype chain inheritance: The parent class private + public as the public; the problem: The parent class is private, in the subclass private + public two places exist;
    • 5) Mixed Inheritance 2:call inheritance + copy inheritance call Inheritance: Take the parent private as its own; Copy inheritance: Through the For In loop, the parent class of the public properties and methods cloned a copy of the same to the subclass of the public;
    • 6) Parasitic combination inheritance: Call Inheritance: The parent class is private as private;
  • Object.create () thought:

    • 1) Create an empty class;
    • 2) The address of the prototype of the parent class is added to the prototype of the empty class, (equivalent to the public properties and methods on the parent prototype, to the empty class prototype)
    • 3) An instance of an empty class is added to the prototype of a subclass (so that subclasses can find the parent class's properties and methods by __proto__, but not by the parent private property)

On the object-oriented summary of JS

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.