Get the App
SLTechnology News&Howtos  ›  Development  › 

What does the JavaScript combination pattern mean?

Shulou Source: shulou.com Published: 2022-06-03 10:05:58 10月03日 Update

This article mainly shows you "what the JavaScript combination pattern refers to", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what the JavaScript combination pattern refers to" this article.

1. The combination pattern combines the objects into a tree structure to represent the part-whole hierarchy.

2. In addition to representing the tree structure, another advantage of the combination pattern is that through the polymorphic expression of objects, users can use single objects and composite objects consistently.

After using combined mode, it is easy to scan the entire folder, we only need to manipulate the objects at the top of the tree.

Example

Var Folder = function (name) {this.name = name; this.files = [];}; Folder.prototype.add = function (file) {this.files.push (file);}; Folder.prototype.scan = function () {console.log ('start scanning folder:' + this.name); for (var I = 0, file, files = this.files; file = files ) {file.scan ();}}; var File = function (name) {this.name = name;}; File.prototype.add = function () {throw new Error ('No more files can be added under the file');}; File.prototype.scan = function () {console.log ('start scanning file:' + this.name);} Next, create some folder and file objects and combine them into a tree. Var folder = new Folder ('learning materials'); var folder1 = new Folder ('JavaScript'); var folder2 = new Folder (' jQuery'); var file1 = new File ('JavaScript design patterns and development practices'); var file2 = new File ('proficient jQuery'); var file3 = new File (' refactoring and patterns') folder1.add (file1); folder2.add (file2); folder.add (folder1); folder.add (folder2); folder.add (file3) The above is all the content of the article "what does the JavaScript combination pattern mean?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Patterns combinations files objects contents folders articles structures learning trees help polymorphism consistency next advantages singles polymorphism learning materials examples hierarchies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Huawei Apple NVidia