Home Web Front-end JS Tutorial JS object-oriented basic explanation (factory mode, constructor mode, prototype mode, mixed mode, dynamic prototype mode)_javascript skills

JS object-oriented basic explanation (factory mode, constructor mode, prototype mode, mixed mode, dynamic prototype mode)_javascript skills

May 16, 2016 pm 04:39 PM
object-oriented

What is object-oriented? Object-oriented is a thought! (nonsense).

Object-oriented can regard the key modules in the program as objects, and the modules have properties and methods. In this way, if we encapsulate some properties and methods, it will be very convenient to use in the future, and we can also avoid tedious and repetitive work. Next, I will explain the object-oriented implementation in JS.

Factory mode

Factory pattern is a well-known design pattern in the field of software engineering. Since classes cannot be created in ECMAScript, function encapsulation is used to create objects with specific interfaces. The implementation method is very simple, that is, create an object within the function, assign properties and methods to the object, and then return the object.

function createBlog(name, url) {
  var o = new Object();
  o.name = name;
  o.url = url;
  o.sayUrl= function() {
    alert(this.url);
  }
  return o;
}

var blog1 = createBlog('wuyuchang', 'http://www.jb51.net/');
Copy after login

You can see that the implementation method of the factory pattern is very simple, which solves the problem of creating multiple similar objects. However, the factory pattern cannot identify the type of objects because they are all Objects, unlike Date, Array, etc., so there is Constructor pattern.

 Constructor Pattern

Constructors in ECMAScript can create objects of specific types, similar to native JS objects such as Array and Date. The implementation method is as follows:

function Blog(name, url) {
  this.name = name;
  this.url = url;
  this.alertUrl = function() {
    alert(this.url);
  }
}

var blog = new Blog('wuyuchang', 'http://www.jb51.net/');
console.log(blog instanceof Blog);  // true, 判断blog是否是Blog的实例,即解决了工厂模式中不能
Copy after login

Except for the different function names between this example and the factory model, careful children should find many differences:

The first letter of the function name should be capitalized (Although the standard does not strictly require that the first letter be capitalized, by convention, the first letter of the constructor should be capitalized
No created object shown
Directly assign properties and methods to this object
No return statement
Use new to create objects
Able to recognize objects (this is where the constructor pattern is better than the factory pattern)

Although the constructor is easy to use, it is not without its shortcomings. The biggest problem with using the constructor is that the method must be re-created every time an instance is created (in theory, the properties of the object are different every time an object is created. The methods of the object are the same), but it is not necessary to create exactly the same method twice, so we can move the function outside the object (maybe some children have already seen the shortcomings, boo!).

function Blog(name, url) {
  this.name = name;
  this.url = url;
  this.alertUrl = alertUrl;
}

function alertUrl() {
  alert(this.url);
}

var blog = new Blog('scjb51', 'http://sc.jb51.net/'),
  blog2 = new Blog('jb51', 'http://www.jb51.net/');
blog.alertUrl();  // http://sc.jb51.net/
blog2.alertUrl();  // http://www.jb51.net/
Copy after login

We set alertUrl as a global function, so that blog and blog2 access the same function. But the problem comes again. We define a function in the global scope that is actually only used by Blog. The display allows The global scope is somewhat worthy of its name. What is even more unacceptable is that many methods are defined in the global scope that are only used by specific objects. Not to mention wasting space, it obviously loses object-oriented encapsulation, so this can be solved through prototypes. question.

 Prototype mode

Every function we create has a prototype attribute, which is a pointer to an object, and the purpose of this object is to contain properties and methods that can be shared by all instances of a specific type. The advantage of using a prototype object is that all object instances can share the properties and methods it contains.

function Blog() {
}

Blog.prototype.name = 'wuyuchang';
Blog.prototype.url = 'http://tools.jb51.net/';
Blog.prototype.friend = ['fr1', 'fr2', 'fr3', 'fr4'];
Blog.prototype.alertInfo = function() {
  alert(this.name + this.url + this.friend );
}

// 以下为测试代码
var blog = new Blog(),
  blog2 = new Blog();
blog.alertInfo();  // wuyuchanghttp://tools.jb51.net/fr1,fr2,fr3,fr4
blog2.alertInfo();  // wuyuchanghttp://tools.jb51.net/fr1,fr2,fr3,fr4

blog.name = 'wyc1';
blog.url = 'http://***.com';
blog.friend.pop();
blog2.name = 'wyc2';
blog2.url = 'http://+++.com';
blog.alertInfo();  // wyc1http://***.comfr1,fr2,fr3
blog2.alertInfo();  // wyc2http://+++.comfr1,fr2,fr3
Copy after login

The prototype pattern is not without its shortcomings. First of all, it omits the constructor to pass initialization parameters. As a result, all instances obtain the same attribute values ​​​​by default. This is very inconvenient, but it is still not a prototype. The biggest problem, the biggest problem with the prototype pattern is caused by the nature of sharing. Because of sharing, if one instance modifies the reference, the other also changes the reference. Therefore, we usually do not use prototypes alone, but combine prototype pattern and constructor pattern.

 Mixed mode (prototype mode, constructor mode)

function Blog(name, url, friend) {
  this.name = name;
  this.url = url;
  this.friend = friend;
}

Blog.prototype.alertInfo = function() {
  alert(this.name + this.url + this.friend);
}

var blog = new Blog('wuyuchang', 'http://tools.jb51.net/', ['fr1', 'fr2', 'fr3']),
  blog2 = new Blog('wyc', 'http://**.com', ['a', 'b']);

blog.friend.pop();
blog.alertInfo();  // wuyuchanghttp://tools.jb51.net/fr1,fr2
blog2.alertInfo();  // wychttp://**.coma,b
Copy after login

In mixed mode, constructor mode is used to define instance properties, while prototype mode is used to define methods and shared properties. Each instance will have its own instance attributes, but at the same time share methods, saving memory to the maximum extent. In addition, this mode also supports passing initial parameters. The advantages are many. This mode is the most widely used and recognized method of creating custom objects in ECMAScript.

 Dynamic Prototype Mode

The dynamic prototype mode encapsulates all information in the constructor, and by initializing the prototype in the constructor (only the prototype is initialized when the first object is instantiated), you can choose whether to initialize the prototype by judging whether the method is valid. .

function Blog(name, url) {
  this.name = name;
  this.url = url;

  if (typeof this.alertInfo != 'function') {
    // 这段代码只执行了一次
    alert('exe time');
    Blog.prototype.alertInfo = function() {
      alert(thia.name + this.url);
    }
  }
}

var blog = new Blog('wuyuchang', 'http://tools.jb51.net'),
  blog2 = new Blog('wyc', 'http:***.com');
Copy after login

You can see that in the above example, the window only pops up once, 'exe time', that is, when the blog is initialized. In this way, blog2 no longer needs to initialize the prototype. It is perfect for creating objects using this mode.

This blog post refers to the 3rd edition of "Advanced Programming with JavaScript ", but the language has been simplified and the examples have been rewritten. If there is anything you don't understand, please leave a reply and the author will update the blog.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement object-oriented event-driven programming using Go language How to implement object-oriented event-driven programming using Go language Jul 20, 2023 pm 10:36 PM

How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

What is the importance of @JsonIdentityInfo annotation using Jackson in Java? What is the importance of @JsonIdentityInfo annotation using Jackson in Java? Sep 23, 2023 am 09:37 AM

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Aug 14, 2023 pm 05:25 PM

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

C# development experience sharing: object-oriented programming and design principles C# development experience sharing: object-oriented programming and design principles Nov 22, 2023 am 08:18 AM

C# (CSharp) is a powerful and popular object-oriented programming language that is widely used in the field of software development. During the C# development process, it is very important to understand the basic concepts and design principles of object-oriented programming (OOP). Object-oriented programming is a programming paradigm that abstracts things in the real world into objects and implements system functions through interactions between objects. In C#, classes are the basic building blocks of object-oriented programming and are used to define the properties and behavior of objects. When developing C#, there are several important design principles

See all articles