JavaScript Patterns and Node Idioms by Rob Richardson

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Advertisements

JavaScript and OSGi Simon Kaegi, Orion Project Co-lead IBM
© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors.
JavaScript: JS301 Week 1: An Introduction to JavaScript.
Windows 8 Metro / HTML5 JS “First Blood” Peter Kellner Microsoft MVP, ASP.NET ASPInsider Primary Organizer Silicon Valley Code.
CSCI 3100 Tutorial 3 JavaScript & Node.js Presented by SU Yuxin Department of Computer Science and Engineering The Chinese University of Hong Kong 1.
Client Side Dev. Toolkit HTML5 JavaScript CSS (and Less, but not today) Bootstrap Knockout.js Require.js (moving to Browserify) Toastr Visual Studio and.
Debugging and Ajax Session 7 INFM 718N Web-Enabled Databases.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQueryAngularJS AngularJS is a client-side JavaScript Framework for adding interactivity to HTML.
JavaScript & jQuery the missing manual Chapter 11
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Using Promise Patterns with JavaScript, jQuery, and the SharePoint 2013 App Model Doug Hemminger SharePoint Solutions Architect, Protiviti.
Chapter 3 : Processing on the Front End JavaScript Technically its name is ECMA-262, which refers to the international standard which defines it. The standard.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Microsoft ASP.net Session Samar Samy Microsoft Student Partner.
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
Using Client-Side Scripts to Enhance Web Applications 1.
JS Basics 1 Lecture JavaScript - Basics. JS Basics 2 What is JavaScript JavaScript is a “simple”, interpreted, programming language with elementary object-
Web Development 101 Presented by John Valance
AMD and RequireJS Splitting JavaScript Code into Dependent Modules Software University Technical Trainers SoftUni Team.
ASP.NET in Definition: 1.ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites,
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Module 4: Creating a Web Application with Web Forms
Plug-in Architectures Presented by Truc Nguyen. What’s a plug-in? “a type of program that tightly integrates with a larger application to add a special.
Esri UC 2014 | Demo Theater | Using ArcGIS Online App Logins in Node.js James Tedrick.
“The world’s most misunderstood language has become the world’s most popular programming language” Akshay Arora
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
1 Javascript CS , Spring What is Javascript ? Browser scripting language  Dynamic page creation  Interactive  Embedded into HTML pages.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
MTA EXAM HTML5 Application Development Fundamentals.
JavaScript for C# developers Dhananjay Microsoft MVP
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
IN THIS LESSON WE WILL REVIEW THE STRUCTURE OF THE INTERNET AND HOW BROWSERS ASSEMBLE WEBSITES BASED ON INSTRUCTIONS THEY RECEIVE FROM SERVERS. Internet.
JavaScript and Ajax Week 10 Web site:
C# Present and Future Marita Paletsou Software Engineer.
Introduction to Node.js® Jitendra Kumar Patel Saturday, January 31, 2015.
Open Solutions for a Changing World™ Eddy Kleinjan Copyright 2005, Data Access WordwideNew Techniques for Building Web Applications June 6-9, 2005 Key.
National College of Science & Information Technology.
Improving Your Application with IntelliTrace #ITDevConnections.
Node.js Modules Header Mastering Node.js, Part 2 Eric W. Greene
JavaScript Introduction
JavaScript: Good Practices
Client Side Dev.
Modules, Babel, RequireJS, Other JavaScript Module Systems
Promises and Asynchronous Programming
Web Systems Development (CSC-215)
Creating Windows Store Apps Using Visual Basic
CSE 154 Lecture 22: AJAX.
Building responsive apps and sites with HTML5 web workers
INTRODUCTION TO By Stepan Vardanyan.
CS3220 Web and Internet Programming JavaScript Basics
Intro to Construct 2 Game Development
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
Lecture 12: The Fetch Api and AJAx
Presentation transcript:

JavaScript Patterns and Node Idioms by Rob Richardson

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob About Me Rob Richardson is a local software craftsman building web properties in ASP.NET and Node. He's a Microsoft MVP, published author, frequent speaker at conferences, user groups, and community events, and a diligent teacher and student of high quality software development. You can find this and other talks on his blog at and follow him on twitter

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob What can client-side JavaScript programmers learn from Node?

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob The most ubiquitous language in the world You probably have a JavaScript runtime in your pocket -- maybe 2 or 3 Every browser since Netscape 4 and IE 4 can run it What is JavaScript

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob Everything in the browser: HTML: content CSS: pretty JavaScript: everything else Why JavaScript

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob We generally see curly braces and just use JavaScript … without learning it

NODE

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob Server-side JavaScript Built on Chrome’s V8 engine Asynchronous to a fault There is no Thread.sleep(); So we use the callback pattern What is Node

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob Promises are built in with ES v. 6, but it’s a higher-level concept and it isn’t the dominant pattern so there’s friction when training others *promises

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob mylib(in, data, function (err, arg, results) { if (err) { return; // handle error } // handle success }); The Callback Pattern

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob The Callback Pattern Benefits Elegant asynchrony Simplest solution Any number of arguments Single result function Drawbacks No run state / handle The airplane wing of nested callbacks

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob lib1(arg1, function (err, cb) { lib2(arg2, function (err, cb) { lib3(arg3, function (err, cb) { lib4(arg4, function (err, cb) { // done }); The Airplane Wing

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob \ \ \ ===================> / / / // weeeeee The Airplane Wing

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob var indata = {some:'data'}; var url = '/path/to/service'; $.getJSON(url, indata, function (outdata) { if (outdata.some !== indata.some) { // take action } }); The Callback Pattern

NODE

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob The Callback Pattern Small modules orchestrated together Package manager: npm Culture of Testing Lessons from Node

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob Single purpose: asynchronously process things Single result: one callback when it’s done Browser and node tests The async module

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob async.parallel([ lib1.bind(null, arg1), lib2.bind(null, arg2), lib3.bind(null, arg3), lib4.bind(null, arg4) ], function (err, results) { if (err) { return; // one of them errored, log, handle } // all of them succeeded }); The async module

JAVASCRIPT

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob or ["prop"] Property: a value in the dictionary Method: a function in the dictionary Dictionaries have no private keys Every object is a Dictionary

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob Think delegate in C#, function pointer in C/C++ Functions define scope, curly braces don’t Every function is an object

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob function myfunc() { console.log(myfunc.data); }; myfunc.data = 2; Every function is an object

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob missing parameters are undefined arguments holds extra parameters Every parameter is optional

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob Truthy / Falsey falsey 0 false "" null undefined NaN truthy everything else … thus ===

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob JavaScript differences Object is Dictionary Every function is an object Functions define scope Every function parameter is optional Truthy / Falsey

PROTECT THE GLOBAL SCOPE

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob window is a dictionary too What if we both name it calendar ? What if we both name it i ? Why Protect Global Scope?

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob 0. Head in the sand

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob var YAHOO = {}; YAHOO.dept = {}; YAHOO.dept.product = {}; //... if (theirVar === YAHOO.dept.product.feature.someEnum.value) { 1. Nested objects

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob (function () { // <-- no name, out of dict. // functions define scope // therefore "private" variables }()); // <-- execute it right away 2. IIFE

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob var myModule = (function () { // private variables return { the: interface }; }()); 3. Revealing Module Pattern

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob CommonJS: node, browserify AMD: requirejs ES6 modules: angular 2 4. Module loaders

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob 4. Module loaders

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob 0. Head in the Sand 1. Nested objects 2. IIFE 3. Revealing Module Pattern 4. Module loaders Protect Global Scope

JAVASCRIPT PATTERNS AND NODE IDIOMS by Rob JavaScript has come of age We can learn from Node

Rate This Session Now! Rate with Mobile App: Select the session from the Agenda or Speakers menus Select the Actions tab Click Rate Session Rate with Website: Register at Go to Select this session from the list and rate it Tell Us What You Thought of This Session Be Entered to WIN Prizes!