Book Image

JavaScript for .NET Developers

By : Ovais Mehboob Ahmed Khan
Book Image

JavaScript for .NET Developers

By: Ovais Mehboob Ahmed Khan

Overview of this book

If you want to improve responsiveness or the UX in your ASP.NET applications, JavaScript can be a life saver. In an age where server-side operations have shifted to the client, being able to handle JavaScript with confidence and fluency is vital for ASP.NET developers. There’s no point trying to fight it, so start learning with this book. Make sure your projects exceed user expectations. Begin by getting stuck into the basics of JavaScript, and explore the language in the context of ASP.NET Core. You’ll then find out how to put the principles into practice, as you learn how to develop a basic ASP.NET application using Angular 2 and TypeScript. You’ll also develop essential skills required to develop responsive apps, with a little help from AJAX, ensuring that you’re building projects that can be easily accessed across different devices. With guidance on Node.js and some neat techniques to test and debug a range of JavaScript libraries in Visual Studio, you’ll soon be well on your way to combining JavaScript with ASP.NET in a way that’s capable of meeting the challenges of modern web development head-on.
Table of Contents (17 chapters)
JavaScript for .NET Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

This is a book about the JavaScript programming language, and is targeted at .NET developers who want to develop responsive web applications using popular client-side JavaScript-based frameworks and create a rich user experience. It is also intended for programmers who have a basic knowledge of the JavaScript programming language and wanted to learn some core and advanced concepts followed by some industry-wide best practices and patterns to structure and design web applications.

This book starts with the basics of JavaScript and helps the reader to gain knowledge about the core concepts and then proceeds towards some advanced topics. There is a chapter that primarily focuses on the jQuery library, which is widely used throughout web application development, followed by a chapter on Ajax techniques that help developers to understand how asynchronous requests can be made. This is followed by the options to make requests either through the plain vanilla JavaScript XHR object or through the jQuery library. There is also a chapter that develops a complete application using Angular 2 and ASP.NET Core, and introduces TypeScript, a superset of JavaScript that supports the latest and evolving features of ECMAScript 2015. We will also explore the Windows JavaScript (WinJS) library to develop Windows applications using JavaScript and HTML and use this library to bring Windows behavior, look, and feel to ASP.NET web applications. There is a complete chapter on Node.js that helps developers to learn how powerful the JavaScript language is on the server side, followed by a chapter on using JavaScript in a large-scale project. Finally, this book will end with a chapter about testing and debugging and discuss what testing suites and debugging techniques are there to troubleshoot and make an application robust.

This book has some very dense topics that require full concentration, hence is ideal for someone having some prior knowledge. All the chapters are related to JavaScript and work around JavaScript frameworks and libraries to build rich web applications. With this book, the reader will get the end-to-end knowledge about the JavaScript language and its frameworks and libraries built on top of it, followed by the techniques to test and troubleshoot the JavaScript code.

What this book covers

Chapter 1, JavaScript for Modern Web Applications, focuses on the basic concepts of JavaScript that involve declaration of variables, datatypes, implementing arrays, expressions, operators, and functions. We will write simple programs in JavaScript using Visual Studio 2015, and see what this IDE offers for writing JavaScript programs. We will also study how JavaScript code can be written and compare the .NET runtime with the JavaScript runtime to clarify the execution cycle of code-compilation process.

Chapter 2, Advanced JavaScript Concepts, covers the advanced concepts of JavaScript and gives developers an insight into the JavaScript language. It will show the extent to which the JavaScript language can be used as far as features are concerned. We will discuss variables hoisting and their scope, property descriptors, object-oriented programming, closures, typed arrays, and exception handling.

Chapter 3, Using jQuery in ASP.NET, discusses jQuery and how to use it in web applications developed in ASP.NET Core. We will discuss the options jQuery provides and the advantages it has when comparing it with the plain vanilla JavaScript for manipulating DOM elements, attaching events, and performing complex operations.

Chapter 4, Ajax Techniques, discusses the techniques of making asynchronous requests known as Ajax requests. We will explore the core concepts of using the XMLHttpRequest (XHR) object and study the basic architecture of how Ajax request is processed and the events and methods it provides. On the other hand, we will also explore what the jQuery library provides in comparison with the plain XHR object.

Chapter 5, Developing an ASP.NET Application Using Angular 2 and Web API, teaches the basic concepts of TypeScript and uses it with Angular 2. We will develop a simple application in ASP.NET Core using Angular 2 as a frontend client-side framework, Web API for backend services, and Entity Framework Core for database persistence. At the time of writing, Angular 2 was in a beta version, and we have used the beta version in this chapter. With the future releases of Angular 2, there are chances of having some changes in the framework, but the basic concepts will almost be the same. For future updates, you can refer to http://angular.io/.

Chapter 6, Exploring the WinJS Library, explores the Microsoft developed WinJS library, which is a JavaScript library to not only develop Windows applications using JavaScript and HTML, but also use it with ASP.NET and other web frameworks. We will discuss the core concepts of defining classes, namespaces, deriving classes, mixins, and promises. We will also look into the data-binding techniques and how to use Windows controls or specific attributes in HTML elements to change the behaviour, look, and feel of the control. Moreover, we will end up using the WinRT API to access a device's camera in our web application and discuss the concepts of a Hosted app through which any web application can be transformed into a Windows application using a Universal Window template in Visual Studio 2015.

Chapter 7, JavaScript Design Patterns, shows that design patterns provide efficient solutions to software design. We will discuss some of the industry-wide best design patterns spread into creational, structural, and behavioral categories. Each category will be covering four types of design patterns that can be used and implemented using JavaScript to solve a particular design problem.

Chapter 8, Node.js for ASP.NET Developers, focuses on the basics of Node.js and how to use it to develop server-side applications using JavaScript. In this chapter, we will discuss view engines such as EJS and Jade and the use of controllers and services to implement the MVC pattern. Moreover, we will end this chapter by performing some examples of accessing a Microsoft SQL Server database to perform, create, and retrieve operations on a database.

Chapter 9, Using JavaScript for Large-Scale Projects, provides best practices of using JavaScript for large-scale applications. We will discuss how to structure our JavaScript-based projects by splitting them into modules to increase the scalability and maintainability. We will see how effectively we can use the Mediator pattern to provide communication between modules and the documentation frameworks that increase the maintainability of your JavaScript code. Finally, we will discuss how the application can be optimized by compressing and merging JavaScript files into a minified version and increase performance.

Chapter 10, Testing and Debugging JavaScript, focuses on the testing and debugging JavaScript applications. We will discuss one of the most popular testing suites of JavaScript code known as Jasmine, and use it with Karma to run the test cases. For debugging, we will discuss some tips and techniques to debug JavaScript with Visual Studio and what Microsoft Edge offers to make debugging easy. In the end, we will study the basic concepts of how Microsoft Edge enables debugging for TypeScript files and the configuration needed to achieve it.

What you need for this book

Throughout the book, we will be using Visual Studio 2015 to practice examples. For the server-side technology, we have used ASP.NET Core for web application development, and used JavaScript on top of it. In Chapter 8, Node.js for ASP.NET Developers, we used Node.js to show how JavaScript can be used on the server side. For Node.js, we will require some extensions for Visual Studio 2015 to be installed, and the details are specified in the chapter.

Who this book is for

This book is targeted at .NET developers who have solid programming experience in ASP.NET Core. Throughout this book, we have used ASP.NET Core for web development and assumed that developers have thorough knowledge or working experience in .NET Core and ASP.NET Core.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "JavaScript can be placed in the <head> or <body> sections of your HTML page."

A block of code is set as follows:

<html>
  <head>
    <script>
      alert("This is a simple text");
    </script>
  </head>
</html>

Any command-line input or output is written as follows:

dotnet ef database update –verbose

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "When the page loads, it will show the pop-up message and a text as This is a simple text."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/JavaScript-For-.NET-Developers. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/JavaScriptForNETDevelopers_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.