وبلاگ بلیان

Modular programming with JavaScript : modularize your JavaScript code for better readability, greater maintainability, and enhanced testability

معرفی کتاب «Modular programming with JavaScript : modularize your JavaScript code for better readability, greater maintainability, and enhanced testability» نوشتهٔ Sasan Seydnejad، منتشرشده توسط نشر Packt Publishing - ebooks Account در سال 2016. این کتاب در 5 صفحه، فرمت pdf، زبان انگلیسی ارائه شده است. «Modular programming with JavaScript : modularize your JavaScript code for better readability, greater maintainability, and enhanced testability» در دستهٔ بدون دسته‌بندی قرار دارد.

Sasan Seydnejad has more than a decade of experience in web UI and frontend application development using JavaScript, CSS, and HTM in .NET and ASP.NET environments. He specializes in modular SPA design and implementation, responsive mobile-friendly user interfaces, AJAX, client architecture, and UX design, using HTML5, CSS3, and their related technologies. He implements framework-less and framework-based applications using Node.js, MongoDB, Express.js, and AngularJS. He is the holder of the U.S. patent for a user interface for a multi-dimensional data store-US Patent 6907428. Writing this book has been an interesting journey and a lot of work but rewarding at the same time. I would like to thank my family who has always motivated me to learn and progress in my life as well as to help others along the way. I would also like to thank my friends, my managers, my colleagues and all the other people who have been a positive influence in my life. You have helped me to grow personally and professionally. Special thanks to my friend Taswar Bhatti for encouraging me to write this book and the book's Content Development Editor, Mayur Pawanikar for keeping me focused on the goal. Also, my book reviewers and all the people in PACKT publishing who made this book possible. My gratitude to JavaScript and open source community as a whole, thank you for your support through out the years. ## About the Reviewers Joe Dorocak, whose Internet moniker is Joe Codeswell, is a very experienced programmer. He enjoys creating readable code that implements the project requirements efficiently and understandably. He considers writing code akin to writing poetry. Joe prides himself on the ability to communicate clearly and professionally. He considers his code to be a form of communication, not only with the machine platforms upon which it will run, but also with all the human programmers who will read it in the future. Joe has been employed as a direct employee or a contractor by IBM, HP, GTE/ Sprint, and other top-shelf companies. He is presently concentrating on the web app and web project consulting, coding primarily, but not exclusively, in Python and JavaScript. For more details, visit https://www.linkedin.com/in/joedorocak. Muhammad Piracha is director of engineering at Bamboo Solutions Corporation, based in Reston, Virginia. Bamboo Solutions is a leading provider of software solutions for the Microsoft® SharePoint® platform. Muhammad has over 18 years experience building document management software applications using various Microsoft products. Currently he oversees several teams across engineering, research and quality assurance to constantly improve the existing portfolio of over 50 products. He is a major driving force in helping the company to develop strategy, architecture, and direction for new initiatives in the SharePoint Online and Office 365TM space. ## www.PacktPub.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Cover 1 Copyright 3 Credits 4 About the Author 5 About the Reviewer 6 www.PacktPub.com 7 Table of Contents 8 Preface 14 Chapter 1: What Are Modules and Why Are They So Good? 20 Prelude to modular programming 21 The simple rule for creating modules 22 A real-life example of modules 22 A look at a non-modular example 24 Re-factoring to a more modularized approach 25 Designing in a modular way 26 Summary 28 Chapter 2: Review of Important JavaScript OOP Concepts 30 JavaScript objects 31 Object type constructors 31 Object literal notation 32 Function objects 33 Functions as object constructors (classes) 33 Functions as static objects 34 Object literal notation versus function objects 35 The "this" keyword 38 Closures 41 Encapsulation and scoping 43 Inheritance 46 Prototype chaining 47 Constructor stealing 51 Parasitic combination inheritance 52 Constructor property 54 Native support for inheritance 55 Summary 57 Chapter 3: Module Design Pattern 58 Module pattern 58 Internal private scope in module pattern 61 Adding properties to an interface in module pattern 63 Object factory module 64 Creating loose coupling among modules 67 Application core module 69 Application Mediator module 70 Application non-core modules 70 testModule1 implementation 70 testModule2 implementation 71 Auto-initialization of application modules 72 Module initialization and design considerations 74 Summary 75 Chapter 4: Designing Simple Modules 76 The big picture 77 Our application requirements 77 Dynamic views 78 Designing our SPA main pieces 79 Application controller 80 Application controller modules 81 Creating our first core module 82 Structure of our first module 82 Using our first module's functionality 85 Mapping our module's methods to its interface 86 Application view 87 Creating the index.html page skeleton 88 Creating an object definition for the header 89 Generating the Header fragment dynamically 90 Generating client application's view dynamically 91 Application model 92 Creating a module for our application's model 93 Creating a logging module 95 Summary 97 Chapter 5: Module Augmentation 98 Principles of module augmentation 98 Implementing module augmentation 99 Simple augmentation of ImagesInc_GlobalData 101 The order of things in module augmentation 102 Loose augmentation of modules 102 Loose augmentation of ImagesInc_GlobalData 104 Testing ImagesInc_GlobalData data encapsulation 107 Tight augmentation of modules 108 Tight augmentation of ImagesInc_GlobalData 108 Loading ImagesInc_GlobalData augmenting code 111 Considerations in tight augmentation 112 Generating our application's content area 113 Summary 116 Chapter 6: Cloning, Inheritance, and Submodules 118 Cloning modules 119 Creating instances of a constructor function 119 Using the assignment operator for copying objects 121 Creating a clone of an object 122 Shallow cloning and deep cloning 123 Using external libraries for cloning 123 Using JSON exploit for cloning 124 Creating a custom clone method 124 Creating ImagesInc_Utilites module 125 Testing our custom cloning method in ImagesInc_Utilites module 127 An important aspect of our cloning method 128 Inheritance in modules 130 Module inheritance using __proto__ object 130 Module inheritance using parasitic combination 132 Deciding on the module inheritance approach 135 Submodules 135 Adding submodules using dynamic properties 136 Adding submodules using asynchronous properties 137 Summary 139 Chapter 7: Base, Sandbox, and Core 140 Application architecture overview 141 Base module 143 Adding general-purpose libraries to the base module 144 Sandbox module 145 Sandbox module functionality 146 Sandbox as a consistent interface 146 Sandbox as a security layer 147 Sandbox as a communication layer 147 Sandbox as a filter 147 Implementing multiple instances of the sandbox module 148 Advantages of multiple instances of the sandbox 148 Core module 151 Core module construction approach 152 Core module functionality 154 Acting as the controller 154 Providing communication bridges 155 Initializing and destroying components 157 Providing plug-and-play capability 159 Providing a centralized approach to handling errors 161 Providing application-level extensibility 163 Leveraging third party libraries 164 Components 167 Components required methods 168 Binding events to the components elements 169 Component design considerations 170 Application architectural considerations 171 Summary 173 Chapter 8: Application Implementation – Putting It All Together 174 The user's view of our application 175 Application functionality 177 Index page 177 Image page 177 Favorites page 178 Support widget 179 Application implementation 181 Loading our module files in index.html 182 Base module implementation 184 Core module implementation 185 MainCore module implementation 186 Component registration with MainCore 191 Dynamic loading of components by MainCore 194 Routing functionality in MainCore module 199 Publish-subscribe implementation in MainCore 202 SandBox module implementation 205 SandBox module's constructor 205 Setting a component's container context in the SandBox instance 206 Identifying the SandBox module's instance 206 SandBox module considerations 207 Application components 207 Registering components with MainCore module 208 Header component 210 Footer component 211 Content component 212 Handling content area click events 212 Content area generator methods 215 NotificationWidget component 215 NotificationWidget's model 215 Required methods of the notificationWidget component 216 Rendering notificationWidget 218 GlobalData module 219 Summary 221 Chapter 9: Modular Application Design and Testing 222 Advantages of writing automated tests 223 Different types of automated tests 224 Unit testing 224 TDD unit tests 224 BDD unit tests 225 TDD versus BDD 225 Test coverage 225 Integration testing 226 End to end testing 226 A simple start to writing our unit tests 227 Writing unit tests without any unit testing frameworks 227 Adding an AppTester module to our application 227 Adding unit test suites to our test-runner 229 CookieHandler module unit tests 229 Running CookieHandler unit tests 231 Cleaning up after running our unit tests 233 Writing unit tests using third party frameworks 234 Introduction to Jasmine 234 Introduction to Mocha 239 Summary 245 Chapter 10: Enterprise Grade Modular Design, AMD, CommonJS, and ES 6 Modules 246 Revisiting index.html file 247 Introducing Asynchronous Module Definition 249 Defining modules using AMD format 249 Importing AMD modules 250 Introducing RequireJS 251 Downloading RequireJS 251 A test project for creating and loading AMD modules 251 Creating a simple AMD module 252 Consuming our person AMD module 253 Loading dependencies of dependencies 254 Loading and consuming non-AMD modules 254 Setting paths in the config object 256 Creating shims for non-AMD modules 256 Refactoring Images Inc. application to use AMD format 257 Modifying Core submodules into AMD modules 257 Loading our non-AMD modules using RequireJS 259 Setting up our application's config.js file 259 Booting up our application using RequireJS 260 CommonJS 262 Implementing a CommonJS module 262 ECMAScript 6 modules 263 Defining an ES6 module 264 Consuming an ES6 module 265 Module loading sequence in the browsers for ES6 modules 265 Summary 267 Index 268

Modularize your JavaScript code for better readability, greater maintainability, and enhanced testability

About This Book

  • Design and build fully modular, modern JavaScript applications using modular design concepts
  • Improve code portability, maintainability, and integrity while creating highly scalable and responsive web applications
  • Implement your own loosely coupled code blocks that can power highly maintainable and powerful applications in a flexible and highly responsive modular architecture

Who This Book Is For

If you are an intermediate to advanced JavaScript developer who has experience of writing JavaScript code but probably not in a modular, portable manner, or you are looking to develop enterprise level JavaScript applications, then this book is for you.

A basic understanding of JavaScript concepts such as OOP, prototypal inheritance, and closures is expected.

What You Will Learn

  • Understand the important concepts of OOP in JavaScript, such as scope, objects, inheritance, event delegation, and more
  • Find out how the module design pattern is used in OOP in JavaScript
  • Design and augment modules using both tight augmentation and loose augmentation
  • Extend the capabilities of modules by creating sub-modules using techniques such as cloning and inheritance
  • Move from isolated module pieces to a cohesive, well integrated application modules that can interact and work together without being tightly coupled
  • See how SandBoxing is used to create a medium for all the modules to talk to each other as well as to the core
  • Use the concepts of modular application design to handle dependencies and load modules asynchronously
  • Become familiar with AMD and CommonJS utilities and discover what the future of JavaScript holds for modular programming and architecture

In Detail

Programming in the modular manner is always encouraged for bigger systems—it is easier to achieve scalability with modular programming. Even JavaScript developers are now interested in building programs in a modular pattern. Modules help people who aren't yet familiar with code to find what they are looking for and also makes it easier for programmers to keep things that are related close together.

Designing and implementing applications in a modular manner is highly encouraged and desirable in both simple and enterprise level applications.

This book covers some real-life examples of modules and how we can translate that into our world of programming and application design. After getting an overview of JavaScript object-oriented programming (OOP) concepts and their practical usage, you should be able to write your own object definitions using the module pattern. You will then learn to design and augment modules and will explore the concepts of cloning, inheritance, sub-modules, and code extensibility. You will also learn about SandBoxing, application design, and architecture based on modular design concepts. Become familiar with AMD and CommonJS utilities.

By the end of the book, you will be able to build spectacular modular applications in JavaScript.

Style and approach

This in-depth step-by-step guide will teach you modular programming with JavaScript. Starting from the basics, it will cover advanced modular patterns that can be used in sophisticated JavaScript applications.

Modularize your JavaScript code for better readability, greater maintainability, and enhanced testabilityAbout This Book Design and build fully modular, modern JavaScript applications using modular design concepts Improve code portability, maintainability, and integrity while creating highly scalable and responsive web applications Implement your own loosely coupled code blocks that can power highly maintainable and powerful applications in a flexible and highly responsive modular architectureWho This Book Is ForIf you are an intermediate to advanced JavaScript developer who has experience of writing JavaScript code but probably not in a modular, portable manner, or you are looking to develop enterprise level JavaScript applications, then this book is for you. A basic understanding of JavaScript concepts such as OOP, prototypal inheritance, and closures is expected. What You Will Learn Understand the important concepts of OOP in JavaScript, such as scope, objects, inheritance, event delegation, and more Find out how the module design pattern is used in OOP in JavaScript Design and augment modules using both tight augmentation and loose augmentation Extend the capabilities of modules by creating sub-modules using techniques such as cloning and inheritance Move from isolated module pieces to a cohesive, well integrated application modules that can interact and work together without being tightly coupled See how SandBoxing is used to create a medium for all the modules to talk to each other as well as to the core Use the concepts of modular application design to handle dependencies and load modules asynchronously Become familiar with AMD and CommonJS utilities and discover what the future of JavaScript holds for modular programming and architectureIn DetailProgramming in the modular manner is always encouraged for bigger systemsit is easier to achieve scalability with modular programming. Even JavaScript developers are now interested in building programs in a modular pattern. Modules help people who aren't yet familiar with code to find what they are looking for and also makes it easier for programmers to keep things that are related close together. Designing and implementing applications in a modular manner is highly encouraged and desirable in both simple and enterprise level applications. This book covers some real-life examples of modules and how we can translate that into our world of programming and application design. After getting an overview of JavaScript object-oriented programming (OOP) concepts and their practical usage, you should be able to write your own object definitions using the module pattern. You will then learn to design and augment modules and will explore the concepts of cloning, inheritance, sub-modules, and code extensibility. You will also learn about SandBoxing, application design, and architecture based on modular design concepts. Become familiar with AMD and CommonJS utilities. By the end of the book, you will be able to build spectacular modular applications in JavaScript. Style and approachThis in-depth step-by-step guide will teach you modular programming with JavaScript. Starting from the basics, it will cover advanced modular patterns that can be used in sophisticated JavaScript applications About This BookDesign and build fully modular, modern JavaScript applications using modular design conceptsImprove code portability, maintainability, and integrity while creating highly scalable and responsive web applicationsImplement your own loosely coupled code blocks that can power highly maintainable and powerful applications in a flexible and highly responsive modular architectureWho This Book Is ForIf you are an intermediate to advanced JavaScript developer who has experience of writing JavaScript code but probably not in a modular, portable manner, or you are looking to develop enterprise-level JavaScript applications, then this book is for you.A basic understanding of JavaScript concepts such as OOP, prototypal inheritance, and closures is expected.What You Will LearnUnderstand the important concepts of OOP in JavaScript, such as scope, objects, inheritance, event delegation, and moreFind out how the module design pattern is used in OOP in JavaScriptDesign and augment modules using both tight augmentation and loose augmentationExtend the capabilities of modules by creating submodules using techniques such as cloning and inheritanceMove from isolated module pieces to cohesive, well integrated application modules that can interact and work together without being tightly coupledSee how SandBoxing is used to create a medium for all the modules to talk to each other as well as to the coreUse the concepts of modular application design to handle dependencies and load modules asynchronouslyBecome familiar with AMD and CommonJS utilities and discover what the future of JavaScript holds for modular programming and architectureIn DetailProgramming in the modular manner is always encouraged for bigger systems―it is easier to achieve scalability with modular programming. Modules help people who aren't yet familiar with code to find what they are looking for and makes it easier for programmers to keep things that are related close together.Designing and implementing applications in a modular manner is highly encouraged and desirable in both simple and enterprise-level applications.This book covers some real-life examples of modules and shows how we can translate them into our world of programming and application design. You will also learn about SandBoxing, application design, and architecture based on modular design concepts. Become familiar with AMD and CommonJS utilities.By the end of the book, you will be able to build spectacular modular applications in JavaScript. Modularize your JavaScript code for better readability, greater maintainability, and enhanced testability About This Book Design and build fully modular, modern JavaScript applications using modular design concepts Improve code portability, maintainability, and integrity while creating highly scalable and responsive web applications Implement your own loosely coupled code blocks that can power highly maintainable and powerful applications in a flexible and highly responsive modular architecture Who This Book Is For If you are an intermediate to advanced JavaScript developer who has experience of writing JavaScript code but probably not in a modular, portable manner, or you are looking to develop enterprise level JavaScript applications, then this book is for you. A basic understanding of JavaScript concepts such as OOP, prototypal inheritance, and closures is expected. What You Will Learn Understand the important concepts of OOP in JavaScript, such as scope, objects, inheritance, event delegation, and more Find out how the module design pattern is used in OOP in JavaScript Design and augment modules using both tight augmentation and loose augmentation Extend the capabilities of modules by creating sub-modules using techniques such as cloning and inheritance Move from isolated module pieces to a cohesive, well integrated application modules that can interact and work together without being tightly coupled See how SandBoxing is used to create a medium for all the modules to talk to each other as well as to the core Use the concepts of modular application design to handle dependencies and load modules asynchronously Become familiar with AMD and CommonJS utilities and discover what the future of JavaScript holds for modular programming and architecture In Detail Programming in the modular manner is always encouraged for bigger systems - it is easier to achieve scalability with modular programming. Even JavaScript developers are now interested in building programs in a modular pattern. Modules help people who aren't yet familiar with code to find what they are looking for and also makes it easier for programmers to keep things that are related close together. Designing and implementing applications in a modular manner is highly encouraged and desirable in both simple and enterprise level applications. This book covers some real-life examples of modules and how we can translate that into our world of programming and application design. After getting..
دانلود کتاب Modular programming with JavaScript : modularize your JavaScript code for better readability, greater maintainability, and enhanced testability