Home  >  Article  >  Web Front-end  >  Detailed explanation of object-oriented methods and advantages and disadvantages of creating objects in JavaScript

Detailed explanation of object-oriented methods and advantages and disadvantages of creating objects in JavaScript

伊谢尔伦
伊谢尔伦Original
2017-07-26 10:48:111262browse

Knowledge points:
(1) JS object-oriented basics: ECMA-262 defines an object as: "A collection of unordered attributes, whose attributes can contain basic values, objects or functions."
(2) Methods of creating objects in JS:
(a) Factory pattern: Use functions to encapsulate the details of creating objects with a specific interface.​​​

 function createPerson(name, age, job){ 
          var o = new Object(); 
          o.name = name; 
          o.age = age; 
          o.job = job; 
          o.sayName = function(){ 
          alert(this.name); 
          }; 
      return o; 
      } 
    var person1 = createPerson(“Nicholas”, 29, “Software Engineer”); 
    var person2 = createPerson(“Greg”, 27, “Doctor”);

​ Disadvantages: Although the factory pattern solves the problem of creating multiple familiar objects, it does not solve the problem of object recognition (that is, how to know the type of an object).
 (b) Constructor pattern: The constructor in ECMAScript can be used to create objects of specific types. You can create custom constructors to define properties and methods of custom object types.​​​

 function Person(name, age, job){ 
        this.name = name; 
        this.age = age; 
        this.job = job; 
        this.sayName = function(){ 
        alert(this.name); 
        }; 
      } 
      var person1 = new Person(“Nicholas”, 29, “Software Engineer”); 
      var person2 = new Person(“Greg”, 27, “Doctor”);

​ Disadvantages: The main problem with using constructors is that each method must be recreated on each instance. Don't forget - functions in ECMAScript are objects, so every time you define a function,
 , you instantiate an object.
 (c) Prototype pattern: Each function we create has a prototype attribute. This attribute is a pointer pointing to an object, and the purpose of this object is to contain all the objects that can be represented by a specific type
  Properties and methods shared by instances. The advantage of using a prototype object is that all objects can share the properties and methods it contains. Disadvantages: All properties in the prototype are shared by many instances. This kind of sharing is very suitable for functions. But for properties with reference type values, the problem is more prominent.

 (d) Combined use of constructor pattern and prototype pattern: The most common way to create a custom type is to use combined use of constructor pattern and prototype pattern. Constructor pattern is used to define instance properties, while prototype pattern is used to define methods and shared properties.​

  function Person(){ 
      } 
      Person.prototype.name = “Nicholas”; 
      Person.prototype.age = 29; 
      Person.prototype.job = “Software Engineer”; 
      Person.prototype.sayName = function(){ 
        alert(this.name); 
      }; 
      var person1 = new Person(); 
      person1.sayName(); //”Nicholas” 
      var person2 = new Person(); 
      person2.sayName(); //”Nicholas” 
      alert(person1.sayName == person2.sayName); //true

The above is the detailed content of Detailed explanation of object-oriented methods and advantages and disadvantages of creating objects in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn