Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Object-oriented programming-factory pattern, constructor pattern

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Factory model

1 function createPerson (name,age,job) {2 var o = new Object (); 3 o.name = name; 4 o.age = age; 5 o.job = job; 6 o.sayName = function () {7 console.log (this.name); 8} 9 return potential 10} 11 12 var person1 = createPerson ("Nicholas", 29, "SoftWare Engineer") 13 var person2 = createPerson ("Greg", 23, "Doctor")

Although the factory pattern solves the problem of creating multiple similar objects, it does not solve the problem of object recognition (that is, how to know the type of an object).

Constructor model

1 function Person (name,age,job) {2 this.name = name; 3 this.age = age; 4 this.job = job; 5 this.sayName = function () {6 console.log (this.name); 7}; 8} 9 10 var person1 = new Person ("Nicholas", 29, "SoftWare Engineer"); 11 var person2 = new Person ("Greg", 23, "Doctor")

In addition to the same part of the code in Person () as in createPerson (), there are the following differences:

1. Created objects that are not displayed

2. Directly assign properties and methods to the this object

3. No return statement

By convention, constructors should always start with an uppercase letter, while non-constructors should start with a lowercase letter.

To create a new instance of Person, you must use the new operator. A constructor called in this way actually goes through four steps

1. Create a new object

2. Assign the scope of the constructor to the new object

3. Execute the code in the constructor (add properties to this new object)

4. Return the new object

Person1 and person2 hold a different instance of Person, respectively. Both objects have a constructor (construct

Function) property, which points to Person

1 alert (person1.constructor = = Person); / / true2 alert (person2.constructor = = Person); / / true

However, when it comes to the type of detection object, the instanceof operator is more reliable, and all the objects created in the above example are

An instance of Object is also an instance of Person.

1 console.log (person1 instanceof Object); / / true2 console.log (person1 instanceof Person); / / true

Creating a custom constructor means that its instance can be identified as a specific type in the future; and this is the constructor pattern

Better than the factory model.

1. Treat the constructor as a function

The only difference between constructors and other functions is the way they are called. Any function that is called through the new operator

Then it can be used as a constructor, and if any function is not called through the new operator, it is no different from an ordinary function.

1 / / as a constructor, use 2 var person = new Person ("Nicholas", 29, "Software Engineer"); 3 person.sayName (); / / Nicholas 4 5 / / call 6 Person ("Greg", 27, "Doctor") as a normal function; 7 window.sayName (); / / Greg 8 9 / / call 10 var o = new Object () in the scope of another object 11 Person.call (o, "Kristen", 25, "Nurse"); 12 o.sayName (); / / Kristen

2. The problem of constructor

The main problem with constructors is that each method is recreated on each instance, and both person1 and person2 have a file named

Methods of sayName (), but those two methods are not the same Function instance

1 console.log (person1.sayName = = person2.sayName); / / false

To solve this problem, the function definition is usually transferred outside the constructor

1 function Person (name,age,job) {2 this.name = name; 3 this.age = age; 4 this.job = job; 5 this.sayName = sayName; 6} 7 8 function sayName () {9 console.log (this.name); 10} 11 12 var person1 = new Person ("Nicholas", 29, "SoftWare Engineer"); 13 var person2 = new Person ("Greg", 23, "Doctor")

As a result, a new problem arises: the function sayName () defined in the global scope can only be called by an object, which makes the global scope a bit unworthy of its name. To make matters worse, if the object needs to define many methods, then many global functions need to be defined, so our custom reference type has no encapsulation at all, and these problems can be solved through the prototype pattern.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report