JavaScript: Good Practices

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Javascript Introduction Norman White Material is from w3schools.com Go there to run examples interactively.
Introduction to JavaScript Module 1 Client Side JS Programming M-GO Sponsored By
12-Jun-15 JavaScript Language Fundamentals I. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was.
Inline, Internal, and External FIle
JavaScript CMPT 281. Outline Introduction to JavaScript Resources What is JavaScript? JavaScript in web pages.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Lesson 19. JavaScript errors Since JavaScript is an interpreted language, syntax errors will usually cause the script to fail. Both browsers will provide.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
1 JavaScript in Context. Server-Side Programming.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
JavaScript Best Practices Learn How to Write Better Quality JavaScript Software University Technical Trainers SoftUni Team.
JavaScript, Fourth Edition
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
1 JavaScript in Context. Server-Side Programming.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
Understanding JavaScript and Coding Essentials Lesson 8.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Functions and Function Expressions Closures, Function Scope, Nested Functions, IIFE Software University Technical Trainers SoftUni Team.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
JavaScript Best Practices Learn How to Write Better Quality JavaScript Software University Technical Trainers SoftUni Team.
Tarik Booker CS 120 California State University, Los Angeles.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Functions and Function Expressions
Introduction to.
Build in Objects In JavaScript, almost "everything" is an object.
Unit 2 Technology Systems
Javascript and Dynamic Web Pages: Client Side Processing
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
Introduction to JavaScript Events
JavaScript Event Handling.
Scope, Objects, Strings, Numbers
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Functions Declarations, Function Expressions and IIFEs
Introduction to JavaScript
Web Development in Microsoft Visual Studio 2013
JavaScript Introduction
Web Programming A different world! Three main languages/tools No Java
A second look at JavaScript
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
PHP.
Functions, Regular expressions and Events
Tonga Institute of Higher Education
Introduction to JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
How to debug a website using IE F12 tools
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Web Programming and Design
Web Programming and Design
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

JavaScript: Good Practices Learn how to write good JavaScript Telerik Software Academy Learning & Development Team http://academy.telerik.com

Table of Contents Naming in JavaScript Scoping Global and function Fake block scope Duplicated Object Identifiers The this object In global, function and object scope In event handler Variables Strict mode

Naming in JavaScript

Naming In JavaScript almost everything is camelCase Variables, methods, properties Objects, modules var number = 5; function printMsg(message){ … } var arr = []; arr.toString(); var controls = (function(){ … } ());

Naming: Function Constructors The only exception to the rule is function constructor Function constructors use PascalCase Function constructors are more special than the other objects They are meant to be called with new Without new, this has an incorrect value Unfortunately, JavaScript has no way to restrict a call to a function constructor without new All we have to do is prey the developer sees the visual difference

Global, Function and Object Scoping Global, Function and Object

Scoping JavaScript has only two types of scope Global scope and function scope Function scope may be called object scope when used with new There is no block scope in JavaScript { and } do not define a scope Use IIFE to define scope All JavaScript code, in all files, share the same global scope

both count and printMsg are defined Scoping Everything inside an if-else, for or while "block scope" is actually outside this block if (false) { var count = 15; function printMsg(message) { console.log("Message: " + message + "!"); }; } printMsg(count) //outputs Message: undefined! both count and printMsg are defined Both printMsg and count are declared Yet, count has no value, because the execution flow cannot reach the initialization

Fake "Block" Scope Live Demo

Function Scope Function scope is the only scope where variables are temporary A variable, declared with var, cannot exist outside of its function scope (function(){ if (false) { var count = 15; function printMsg(message) { console.log("Message: " + message + "!"); }; } }()); printMsg(count); //ReferenceError: printMsg is not defined

Function Scope Live Demo

Duplicated Object Identifiers

Duplicated Object Identifiers Due to the shared global scope in JavaScript, it is a common case to have a duplicated object identifiers What happens if two or more libraries/frameworks/js files have a function with the same name? Which one is the actual?

Duplicated Object Identifiers (2) The solution to preventing duplicated identifiers is using function scope, or maybe a module Do the stuff you need and expose only the meaningful pieces jsConsole.write("Message"); document.write("Message"); database.write("Message");

Duplicated Object Identifiers Live Demo

The this Object

The this Object The this object has a different value depending on the scope In function scope In object scope In global scope In event handlers

this in Global Scope In the global scope this means the global scope i.e. window These work exactly the same when in global scope console.log( this === window) //logs true this.message = "Hello"; var message = "Hello"; window.message = "Hello";

this in Global Scope Live Demo

this in Function Scope this in function scope almost always means the this of the parent of the function If the function is in the global scope this means the global scope In object scope – this means the object itself Later in this presentation (function createAndSetVariable(number){ this.number = number; }(5)); console.log(number); //logs 5 this means window

this in Function Scope Live Demo

this means an instance of the person object this in Object Scope Object scope is created when a function is called with new The rules that apply are the same as with regular function call Except for the value of this function Person(fname, lname){ this.fname = fname; this.lname = lname; } var person = new Person(); var invalidPerson = Person(); this means an instance of the person object this means the window Always beware of PascalCase-named functions There is a reason for that!

this in Object Scope Live Demo

this in Event Handlers this in an event handler means the DOM element that the event was fired on i.e. if a click event fires on a anchor element, this means the clicked anchor element var button = document.getElementById("the-button"); function onButtonClick(ev){ console.log(this === button); //logs true } button.addEventListener("click", onButtonClick, false); var usernameTb = document.getElementById("tb-user"); function onUsernameTbChange(ev){ console.log(this === usernameTb); //logs true } usernameTb.addEventListener("change", onUsernameTbChange, false);

this in Event Handlers Live Demo

Variables

Variables Declare all the variables in the beginning of the scope Even if they will not be used yet This prevents lots of error-prone code Many ways to structure variables declaration: function something(){ var number; var word; var eventNumbers; … } function something(){ var number, word, eventNumbers; … }

Both work exactly the same Both work exactly the same Variables Declare all the variables in the beginning of the scope Even if they will not be used yet This prevents lots of error-prone code Many ways to structure variables declaration: function something(){ var number; var word; var eventNumbers; … } function something(){ var number, word, eventNumbers; … } Both work exactly the same Both work exactly the same

Variables Live Demo

Strict Mode

Strict Mode Strict mode is a nice subset of the JavaScript functionality Removes some of the bad parts of JavaScript Adds parts of yet-to-be ECMAScript versions Strict mode changes both syntax and runtime behavior Makes changes to the syntax to prevent silent errors Restricts functionality to remove bad JS Makes the transition to new JS features more seamless

Strict Mode Usage Strict mode can be used either for the whole script or per-function If used for the whole scripts, everything is in strict mode Not a good idea, since a third-party script may fail in strict mode Better use IIFE and per-function strict mode That way only your code will run in strict mode

Strict Mode Properties Some of the characteristics of Strict mode: Converts silent errors to exceptions Trying to change the value of document Deleting the prototype of an object Makes this undefined inside a function scope In a function scope, this is equal to undefined, instead of the parent this object Forbids octal syntax Prevents variable declaration without var

Strict Mode Live Demo

JavaScript Execution

JavaScript Execution As we know, JavaScript executes per-line- reached basis The execution flow goes from top to bottom Imagine all loaded JavaScript files, merged together in one really big JavaScript file A JavaScript line of code is executed, when it is reached in the execution process Yet execution may take time Time that is not pleasant to the user

JavaScript Execution (2) A common approach is to start execution of JavaScript, when the web page is ready And there is an event for that window.onload = function(){ //do the code preparations } Or, if using jQuery, we can use its load event $(document).ready(function(){}); $(function(){}); Loading the script at the end of the load time, ensures that all the DOM is already rendered

JavaScript Load in the HTML file

JavaScript Load in the HTML File A common questions is "Where to load the JavaScript?" Load it in the header? Load it at the end of the body element? Load it anywhere in the document All JavaScript files have the same global scope, so it really doesn't matter? No, it does matter

JavaScript Load in the HTML File (2) There are two common places to load the JavaScript files In the header At the end of the body element What is really the difference? Put simply – Performance

JavaScript Load in the HTML File (3) Loading of large script file at the document header, freezes the web page, and makes it unusable Better show something to the user Like the rendered HTML and CSS And then load your JavaScript

JavaScript: Good Practices http://academy.telerik.com