Home Web Front-end JS Tutorial Learn JavaScript design patterns (encapsulation)_javascript skills

Learn JavaScript design patterns (encapsulation)_javascript skills

May 16, 2016 pm 03:29 PM
javascript encapsulation Design Patterns

In JavaScript, there is no support for abstract classes and interfaces. JavaScript itself is also a weakly typed language. JavaScript doesn't have the ability or need to do much more when it comes to encapsulating types. For the implementation of design patterns in JavaScript, not distinguishing types is a disgrace, and it can also be said to be a relief.

From the perspective of design patterns, packaging is reflected in packaging changes at a more important level.

By encapsulating changes, we isolate the stable parts of the system from the parts that are easy to change. During the evolution of the system, we only need to replace those parts that are easy to change. If these parts have been encapsulated Yes, it is relatively easy to replace. This can ensure the stability and scalability of the program to the greatest extent.

There are three basic modes of JavaScript encapsulation:

1. Use the principle of agreement priority, and all private variables start with _

 <script type="text/javascript">
  /**
   * 使用约定优先的原则,把所有的私有变量都使用_开头
   */
  var Person = function (no, name, age)
  {
   this.setNo(no);
   this.setName(name);
   this.setAge(age);
  }
  Person.prototype = {
   constructor: Person,
   checkNo: function (no)
   {
    if (!no.constructor == "string" || no.length != 4)
     throw new Error("学号必须为4位");
   },
   setNo: function (no)
   {
    this.checkNo(no);
    this._no = no;
   }, 
   getNo: function ()
   {
    return this._no;
   setName: function (name)
   {
    this._name = name;
   }, 
   getName: function ()
   {
    return this._name;
   }, 
   setAge: function (age)
   {
    this._age = age;
   }, 
   getAge: function ()
   {
    return this._age;
   }, 
   toString: function ()
   {
    return "no = " + this._no + " , name = " + this._name + " , age = " + this._age;
   }
  };
  var p1 = new Person("0001", "小平果", "22");
  console.log(p1.toString());  //no = 0001 , name = 小平果 , age = 22
  p1.setNo("0003");
  console.log(p1.toString());  //no = 0003 , name = 小平果 , age = 22
  p1.no = "0004";
  p1._no = "0004";
  console.log(p1.toString()); //no = 0004 , name =小平果 , age = 22

 </script>
Copy after login

After reading the code, do you feel like you have been cheated? If you just put all the variables starting with _, they can still be accessed directly. Can this be called encapsulation? Of course, it is said that the agreement takes precedence.

This use of the underscore is a well-known naming convention, which indicates that a property is only for internal use of the object, and accessing it or setting it directly may lead to unexpected consequences. This helps prevent programmers from accidentally using it, but it does not prevent it from being used intentionally.

This method is still good. At least the getter and setter methods of member variables are in the prototype, not in the object. Overall, it is a good choice. If you feel that this is not possible and that encapsulation must be strictly implemented, then look at the second method.

2. Strictly implement encapsulation

<script type="text/javascript">
  /**
   * 使用这种方式虽然可以严格实现封装,但是带来的问题是get和set方法都不能存储在prototype中,都是存储在对象中的
   * 这样无形中就增加了开销
   */
  var Person = function (no, name, age)
  {
   var _no , _name, _age ;
   var checkNo = function (no)
   {
    if (!no.constructor == "string" || no.length != 4)
     throw new Error("学号必须为4位");
   };
   this.setNo = function (no)
   {
    checkNo(no);
    _no = no;
   };
   this.getNo = function ()
   {
    return _no;
   }
   this.setName = function (name)
   {
    _name = name;
   }

   this.getName = function ()
   {
    return _name;
   }

   this.setAge = function (age)
   {
    _age = age;
   }
   this.
     getAge = function ()
   {
    return _age;
   }

   this.setNo(no);
   this.setName(name);
   this.setAge(age);
  }
  Person.prototype = {
   constructor: Person,
   toString: function ()
   {
    return "no = " + this.getNo() + " , name = " + this.getName() + " , age = " + this.getAge();
   }
  }
  ;
  var p1 = new Person("0001", "小平果", "22");
  console.log(p1.toString());  //no = 0001 , name =小平果 , age = 22
  p1.setNo("0003");
  console.log(p1.toString());  //no = 0003 , name = 小平果 , age = 22
  p1.no = "0004";
  console.log(p1.toString()); //no = 0003 , name = 小平果 , age = 22

 </script>

Copy after login

So how is this different from other modes of creating objects we have talked about before? In the above example, we always use the this keyword when creating and referencing the properties of an object. In this example, we declare these variables with var. This means they only exist in the Person constructor. The checkno function is declared in the same way and therefore becomes a private method.

Methods that need to access these variables and functions only need to be declared in Person. These methods are called privileged methods because they are public but have access to private properties and methods. In order to access these privileged functions from outside the object, they are preceded by the keyword this. Because these methods are defined in the scope of the Person constructor, they have access to private properties. These properties are referenced without using the this keyword because they are not public. All getter and assigner methods have been changed to reference these properties directly without this.

Any method that does not require direct access to private properties can be declared in Person.prototype as originally. Like toString() method. Only methods that require direct access to private members should be designed as privileged methods. But too many privileged methods take up too much memory, because each object instance contains a new copy of all privileged methods.

Look at the above code, the this. attribute name has been removed, and encapsulation has been strictly implemented. Member variables can only be accessed through getters and setters. However, there is a problem. All methods exist in the object, which increases memory overhead. .

3. Encapsulated in a closure

<script type="text/javascript">

  var Person = (function ()
  {
   //静态方法(共享方法)
   var checkNo = function (no)
   {
    if (!no.constructor == "string" || no.length != 4)
     throw new Error("学号必须为4位");
   };
   //静态变量(共享变量)
   var times = 0;

    //return the constructor.
   return function (no, name, age)
   {
    console.log(times++); // 0 ,1 , 2
    var no , name , age; //私有变量
    this.setNo = function (no) //私有方法
    {
     checkNo(no);
     this._no = no;
    };
    this.getNo = function ()
    {
     return this._no;
    }
    this.setName = function (name)
    {
     this._name = name;
    }

    this.getName = function ()
    {
     return this._name;
    }

    this.setAge = function (age)
    {
     this._age = age;
    }
    this.getAge = function ()
    {
     return this._age;
    }

    this.setNo(no);
    this.setName(name);
    this.setAge(age);
   }
  })();

  Person.prototype = {
   constructor: Person,
   toString: function ()
   {
    return "no = " + this._no + " , name = " + this._name + " , age = " + this._age;
   }
  };

  var p1 = new Person("0001", "小平果", "22");
  var p2 = new Person("0002", "abc", "23");
  var p3 = new Person("0003", "aobama", "24");


  console.log(p1.toString());  //no = 0001 , name = 小平果 , age = 22
  console.log(p2.toString());  //no = 0002 , name = abc , age = 23
  console.log(p3.toString()); //no = 0003 , name = aobama , age = 24

 </script>

Copy after login

The above code, after the js engine is loaded, will directly execute the Person = immediate execution function, and then this function returns a sub-function, which is the constructor called by new Person, and because the sub-function maintains A reference to checkNo(no) and times in the immediate execution function (obvious closure), so checkNo and times are common to all Person objects. After creating three objects, times are 0, 1, and 2 respectively. The advantage of this approach is that the methods and properties that need to be reused in Person can be made private and shared between objects.

The private members and privileged members here are still declared in the constructor. But the constructor has changed from an ordinary function to an inline function, and is given to the variable Person as the return value of the function containing it. This creates a closure in which you can declare static private members. The pair of empty brackets located after the outer function declaration is very important. Its function is to execute the function as soon as the code is loaded. The return value of this function is another function, which is assigned to the Person variable, so Person becomes a constructor. This inner function is called when instantiating Person. The outer function is just used to create a closure that can be used to store static members.

In this example, checkno is designed to be a static method because it makes no sense to generate a new copy of this method for each instance of Person. There is also a static attribute times, whose function is to track the total number of calls to the Person constructor .

The above is the entire content of this article. I hope it will be helpful to everyone's study. You can learn more about the meaning of encapsulation.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

AMD 'Strix Halo” FP11 package size exposed: equivalent to Intel LGA1700, 60% larger than Phoenix AMD 'Strix Halo” FP11 package size exposed: equivalent to Intel LGA1700, 60% larger than Phoenix Jul 18, 2024 am 02:04 AM

This website reported on July 9 that the AMD Zen5 architecture "Strix" series processors will have two packaging solutions. The smaller StrixPoint will use the FP8 package, while the StrixHalo will use the FP11 package. Source: videocardz source @Olrak29_ The latest revelation is that StrixHalo’s FP11 package size is 37.5mm*45mm (1687 square millimeters), which is the same as the LGA-1700 package size of Intel’s AlderLake and RaptorLake CPUs. AMD’s latest Phoenix APU uses an FP8 packaging solution with a size of 25*40mm, which means that StrixHalo’s F

The difference between design patterns and architectural patterns in Java framework The difference between design patterns and architectural patterns in Java framework Jun 02, 2024 pm 12:59 PM

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

Foxconn builds AI one-stop service, and invested Sharp to enter advanced semiconductor packaging: put into production in 2026, designed to produce 20,000 wafers per month Foxconn builds AI one-stop service, and invested Sharp to enter advanced semiconductor packaging: put into production in 2026, designed to produce 20,000 wafers per month Jul 18, 2024 pm 02:17 PM

According to news from this site on July 11, the Economic Daily reported today (July 11) that Foxconn Group has entered the advanced packaging field, focusing on the current mainstream panel-level fan-out packaging (FOPLP) semiconductor solution. 1. Following its subsidiary Innolux, Sharp, invested by Foxconn Group, also announced its entry into Japan's panel-level fan-out packaging field and is expected to be put into production in 2026. Foxconn Group itself has sufficient influence in the AI ​​field, and by making up for its shortcomings in advanced packaging, it can provide "one-stop" services to facilitate the acceptance of more AI product orders in the future. According to public information consulted on this site, Foxconn Group currently holds 10.5% of Sharp's shares. The group stated that it will not increase or reduce its holdings at this stage and will maintain its holdings.

Analysis of the Decorator Pattern in Java Design Patterns Analysis of the Decorator Pattern in Java Design Patterns May 09, 2024 pm 03:12 PM

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

How design patterns deal with code maintenance challenges How design patterns deal with code maintenance challenges May 09, 2024 pm 12:45 PM

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

PHP design pattern practical case analysis PHP design pattern practical case analysis May 08, 2024 am 08:09 AM

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

The wonderful use of the adapter pattern in Java design patterns The wonderful use of the adapter pattern in Java design patterns May 09, 2024 pm 12:54 PM

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

PHP Design Patterns: Test Driven Development in Practice PHP Design Patterns: Test Driven Development in Practice Jun 03, 2024 pm 02:14 PM

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

See all articles