Home  >  Article  >  Web Front-end  >  What are the modes for creating objects?

What are the modes for creating objects?

零下一度
零下一度Original
2017-06-26 11:29:271453browse

1 new Object()

First create an Object instance, and then add properties and methods to it

var Person = new Object()
Person.name = 'hl'
Person.sayName = function () {
  console.log(this.name)
}

2 Object literal method

The object literal method is The fastest and most convenient way to create objects, it is used in many scenarios.

var Person = {
  name: 'hl',
  sayName: function () {
    console.log(this.name)
  }
}

The disadvantage of the object literal method is that when creating multiple objects of the same type, a lot of duplicate code will be generated, so there is a factory pattern.

3 Factory Pattern

Factory pattern uses functions to encapsulate the details of creating objects. When calling the function, pass in the object properties and then return an object.

function createPerson (name) {
  return {
    name: name,
    sayName: function () {
      console.log(this.name)
    }
  }
}
var person = createPerson('hl')
var person = new createPerson('hl') // 寄生构造函数模式

The same result can also be obtained by using the new operator. This method is called the parasitic constructor pattern, and (should) be no different from calling the function directly.
Although the factory pattern solves the problem of creating multiple objects of the same type, it cannot identify the specific type of the object.

4 Constructor pattern

For objects created through the constructor, you can use the instanceof operator to determine the type of the object. According to programming convention, constructor names should be capitalized to distinguish them from ordinary functions.

function Person (name) {
  this.name = name
  this.sayName = function () {
    console.log(this.name)
  }
}
p = new Person('hl')
p instanceof Person // true

Characteristics of the constructor:

  • No explicit creation of objects

  • Attributes and methods are directly assigned to this

  • No return statement

  • Use the new operator to create objects

The disadvantage of the constructor is that each The method will be recreated on each instance, causing a waste of memory.

5 Prototype pattern

Using the prototype pattern, you can easily add properties and methods to objects.

function Person () {
}
var p = new Person()
Person.prototype.name = 'hl'
Person.prototype.sayName = function () {
  console.log(this.name)
}
p.sayName() // hl

The prototype is dynamic, that is, the object is created first and then the prototype is modified, and the instance can also obtain the corresponding properties and methods.
The prototype mode is not without its shortcomings. First, the prototype mode cannot pass initialization parameters, causing each instance to obtain the same properties; second, for reference type values, all instances refer to the same object, see below Example:

function Person () {
}
Person.prototype.relative = ['father','mother']

var person1 = new Person()
var person2 = new Person()
person1.relative.push('sister')
console.log(person2.relative) // [ 'father', 'mother', 'sister' ]

Modify the attributes of person1, and the attributes of person2 are also modified. Instances generally need to have their own attributes, so the prototype pattern is rarely used alone.

6 Combine the constructor pattern and the prototype pattern

The most common way to create objects is to combine the constructor pattern and the prototype pattern. Constructors are used for custom properties, and prototype patterns are used to define shared properties and methods.

function Person (name) {
  this.name = name
}
Person.prototype.sayName = function () {
  console.log(this.name)
}

7 Dynamic prototype mode

The prototype can be initialized in the constructor to better encapsulate the object creation process.

function Person(name) {
  this.name = name
  if (typeof this.sayName !== 'function') {
    Person.prototype.setName= function (name) {
      this.name = name
    }
    Person.prototype.sayName = function () {
      console.log(this.name)
    }
  }
}

You don’t have to use if to check every property or method, you only need to check one of the properties or methods that should exist after the prototype is initialized.

8 Safe constructor pattern

A safe object means that it has no public properties, its properties and methods do not reference this object, and the new operator is not used to create objects. Suitable for use in some environments that require security to prevent data from being modified.

function Person (name) {
  return {
    sayName: function () {
      console.log(name)
    }
  }
}
var person = Person('hl')

Objects created in safe mode have no way to modify and access the original data passed into the constructor except using the methods defined in the constructor.

The above is the detailed content of What are the modes for creating objects?. 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