JavaScript or: HOW I LEARNED TO STOP WORRYING AND LOVE A CLASSLESS*

Similar documents
JavaScript or: How I Learned to Stop Worrying and Love a Classless Loosely Typed Language

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

Intro To Javascript. Intro to Web Development

a Very Short Introduction to AngularJS

JavaScript: Sort of a Big Deal,

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Client Side JavaScript and AJAX

JavaScript for C# Programmers Kevin

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Comprehensive AngularJS Programming (5 Days)

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

SEEM4570 System Design and Implementation Lecture 03 JavaScript

Web Application Development

Javascript. Many examples from Kyle Simpson: Scope and Closures

Welcome to CS50 section! This is Week 10 :(

Client-Side Web Technologies. JavaScript Part I

(Refer Slide Time: 4:00)

AngularJS Fundamentals

INF5750. Introduction to JavaScript and Node.js

JavaScript for PHP Developers

welcome to BOILERCAMP HOW TO WEB DEV

Choosing the web s future. Peter-Paul Koch Van Lanschot, 9 February 2017

JSON as an XML Alternative. JSON is a light-weight alternative to XML for datainterchange

JavaScript Lecture 1

What is Node.js? Tim Davis Director, The Turtle Partnership Ltd

JSON is a light-weight alternative to XML for data-interchange JSON = JavaScript Object Notation

Node.js Training JavaScript. Richard richardrodger.com

Intro. Scheme Basics. scm> 5 5. scm>

P a g e 1. Danish Technological Institute. Scripting and Web Languages Online Course k Scripting and Web Languages

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains 2 components

Why Discuss JavaScript? CS312: Programming Languages. Lecture 21: JavaScript. JavaScript Target. What s a Scripting Language?

CS312: Programming Languages. Lecture 21: JavaScript

COMP-520 GoLite Tutorial

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

Module 5 JavaScript, AJAX, and jquery. Module 5. Module 5 Contains an Individual and Group component

CGS 3066: Spring 2015 JavaScript Reference

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

JavaScript. What s wrong with JavaScript?

D Programming Language

Advance Mobile& Web Application development using Angular and Native Script

Web Development for Dinosaurs An Introduction to Modern Web Development

JavaScript Basics. The Big Picture

Chapter 1 Getting Started

LEARN WITH INTRODUCTION TO TYPESCRIPT

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

JAVASCRIPT FOR THE C# DEVELOPER

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

One language to rule them all: TypeScript. Gil Fink CEO and Senior Consultant, sparxys

Week - 01 Lecture - 04 Downloading and installing Python

Functions. Functions in JavaScript are declared using the function keyword

Friday. Last class Some of what we covered: for loop nested for loops toast. The place from which you call a function affects what it can do!

JAVASCRIPT - CREATING A TOC

Project Compiler. CS031 TA Help Session November 28, 2011

JavaScript Fundamentals_

CL_55244 JavaScript for Developers

Future Web App Technologies

JavaScript: Getting Started

Semantic Analysis. Lecture 9. February 7, 2018

Jquery.ajax Call Returns Status Code Of 200 But Fires Jquery Error

Front End Nanodegree Syllabus

Part 1: jquery & History of DOM Scripting

Manoj Kumar- From Call back's hell to using Async Await: Automated testing with JavaScript

Princess Nourah bint Abdulrahman University. Computer Sciences Department

STARCOUNTER. Technical Overview

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

JavaScript Introduction

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

JAVA: A Primer. By: Amrita Rajagopal

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Upcoming Features in C# Mads Torgersen, MSFT

377 Student Guide to C++

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

A Brief Introduction to a web browser's HTML. and the Document Object Model (DOM) Lecture 28

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Chapter 6 Classes and Objects

Lecture 2, September 4

ES6 Modules...& Polymer

High Performance Single Page Application with Vue.js

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Quick.JS Documentation

Today. Continue our very basic intro to JavaScript. Lambda calculus

JavaScript Functions, Objects and Array

Front-End Web Developer Nanodegree Syllabus

iframe programming with jquery jquery Summit 2011

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

Programming Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science Indian Institute of Technology, Madras

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

HTML CSS JAVASCRIPT WEB PUBLISHING IN ONE HOUR A DAY SAMS TEACH YOURSELF COVERING HTML5 CSS3 AND JQUERY 7TH EDITION

User Interaction: jquery

JavaScript: the Big Picture

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

AngularJS Intro Homework

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

An Introduction to Stored Procedures in MySQL 5 by Federico Leven6 Apr 2011

Powerful JavaScript OOP concept here and now. CoffeeScript, TypeScript, etc

Transcription:

JavaScript or: HOW I LEARNED TO STOP WORRYING AND LOVE A CLASSLESS* LOOSELY TYPED LANGUAGE

Two parts Part 1 JavaScript the language Part 2 What is it good for?

Part 1 JavaScript the language What is JavaScript? Why do people hate JavaScript? / Should *you* hate JavaScript? Do you even need to know JavaScript? How to adapt and overcome if you come from a Java/C# background

About the JavaScript language Some history If its broken, why not fix it? Lives in the browser Very powerful and fast Easy to learn Just got a major upgrade that makes the transition from C# or Java that much easier

JS6 === ECMA Script 2015 Lots of new features (rest-parameters, lambda (arrow) functions, generators) Making syntax more intuitive and hiding more complex patterns with more easily understood interfaces classes and inheritance The let keyword Ability to check if you are out of bounds with your numbers

What are the difficult parts? Loosely typed language / dynamic typing The var/let type Undefined/NaN and other strange results Hoisting Scope this Do not trust the compiler! Everything (almost) is done by functions!

The var/let type An untyped variable, it can be anything! No such thing in Java. C# has it but it is not the same at all since it implicitly sets the variable type when compiling. var or let? let a = 1; a = {}; a = hello ; // number // object // string

Hoisting and coalescing Hoisting All variable declaration takes place first in a function no matter where they are actually written. Coalescing All variables tries to adapt to their surroundings what am I supposed to be now? Can I be a number, a string? Truthy? Falsy?

Scope Scope is not the same as in Java or C# (ie not bracket defined, unless you use let or try catch ) Everything is in global scope by default! var a = Im a global ; // defined in global scope Scope in JavaScript is defined by... Functions unless you use the let keyword

Truth and truthiness in JavaScript If you tell the truth, you don't have to remember anything. This is especially true when we come to JavaScript. == //truthy!= // falsy === // true!== // false

Truth and truthy 1 == 1 // true false == 0 undefined == null undefined == null == 0 // true // true // false // false == false // true == 0 // true falsy : null, undefined, "", 0, NaN truthy: all the rest Always use === or!== when writing JavaScript!

Functions functions functions... As soon as you do not know how to do something in JS, write a function. All functions return something, a function without a return statement returns undefined. Functions are objects, this means that a function can be stored in a variable, array, or object. Also, a function can be passed to, and returned from, a function.

Functions (are like methods... mostly) Mostly the same as a method in Java or C# // Java public static int addfunction (int val1, int val2) { return (val1+val2); } //JavaScript funciton addfunction(val1,val2) { return (val1+val2); } // or let myfunc = function(val1,val2) { return(val1 +val2); }

Mostly... No overloading last one is always the correct one. No return type but always returns something. No check for how many arguments we pass in. Special property arguments to deal with unspecified number of arguments

Callbacks Used for ajax calls before promises async programming Quite tricky to wrap your head around, sort of like recursion You need to know about them, especially in NodeJS Promises and generators can be a big help if they feel overwhelming

Objects (which may be or contain.. functions) Similar to serialized data, such as JSON var mydotahero { name: Luna, typical role : Carry, skillvalues : { str:15, dex:18, int:16 } }

Objects part 2 To retrieve a value we use dot or bracket notation var name = mycarry.name; var namealt = mycarry["name"]; var str = mycarry.skillvalues.str; We can also add new properties and even functions to our object dynamically mycarry.rival = "Mirana"; mycarry.attack = function () { var target = this.rival; }

this this - the current state of which the method was invoked off of. function changecolor() { } return this; // this points to the browser window var myobject = { firstname:"john", lastname: "Doe", fullname: function () { return this.firstname + " " + this.lastname; } } myobject.fullname(); // this points to the object

Classes New keyword class Class DotaHero { constructor () { } attack() { return( attacked ); } } let magnus = new DotaHero(); console.log(magnus.attack());

The prototype design pattern function Car( model, year, miles ) { this.model = model; this.year = year; this.miles = miles; } Car.prototype.toString = function () { }; return this.model + " has done " + this.miles + " miles"; var mycar = new Car("Volvo"); alert(mycar.tostring());

Generators Yield keyword JavaScript is single threaded Generators gives us cooperative code that may be released and then started up again. function* foo() { var a = yield 1; var b = yield 2; var c =yield 3; } Execellent for async operatoins looks much nicer than promises or callbacks

So... can I use JS6? Yes, otherwise this lecture would have been kinda pointless and we would have talked about prototype patterns and the var keyword etc. No, current browsers does not support it yet (Edge(!)being your best bet). But you can use a transpiler to compile your JS6 into older JS that is runnable in all browsers. Hard to find help since its such a major update, stack overflow has a lot of JS5 help might be a reason to look to a framework instead. For a complete list of browser compability to ES5,6 and 7,go to: https://kangax.github.io/compat-table/es6/

Testing JavaScript Traditionally it has been hard to accurately test front end development code. Not any more! Karma Jasmine QUnit

Alternatives to JavaScript Dart (Google - compiles to JavaScript) TypeScript (MS compiles to JavaScript) GWT (Write in Java compiles to JavaScript) CoffeScript (Compiles to JavaScript) The first three examples are made to make JavaScript more strongly typed. This may or may not be what you want since it also removes a lot of the inherited power of the language.

Part 2 What is it good for? JavaScript is the language we use to speak to the web browser.

The Document Object Model (DOM) One way to represent a webpage and manipulate is through the Document Object Model View the DOM : F12 in the browser of your choice. JavaScript was made for interacting with the DOM!

Wait for the DOM before running JS-code window.onload = function () { // excecutes as soon as the whole page is loaded, including images etc }; document.addeventlistener("domcontentloaded", function(event){ } // executes as soon as the DOM is loaded

DOM manipulation- getting elements By tag name document.getelementsbytagname("input"); By id: document.getelementbyid("id"); By class: document.getelementsbyclassname("classname");

Node lists A nodelist is like an array but is always a resultset from our document.getelements... var nodes = document.getelementsbyclass('myclass'); nodes[0].innerhtml = my own text ; for (var i = 0; i < = nodes.length; i++ ) { }; nodes[i].innerhtml = my own text ; // or [].foreach.call(nodes, function(element, index) { }); element.classname = backlogheading";

JQuery and other frameworks The problem with JavaScript is not the language itself - it's a perfectly good prototyped and dynamic language. If you come from an OO background there's a bit of a learning curve, but it's not the language's fault. The real problem is that it's compiled by the browser, and that means it works in a very different way depending on the client. So man created frameworks to deal with this JQuery being the most common as well as a prerequisite for several others.

Why use JQuery? It seems very simple and easy to understand The almighty $ Easy to pick up and most of all it helps the programmer to tame the DOM manipulation process in different browsers and platforms. $( #myid ).addclass( selectedbackground ); $(.myclass ).click(function(){ }); alert( clicked! );

Why not to use JQuery Don t use it unless you have to support IE8. IE9 can still be managed with polyfills. It is slow and huge in comparison to vanilla JavaScript. Anything you can do in JQuery you can do in JavaScript*. Build your own helper functions when needed instead! * Some things may be a bit difficult though, like $( #myid ).closest( li );

Alternatives to JQuery Some people really like their $ so if you feel you really need it, there are alternatives such as: Zepto MinJS Build a custom JQuery build with the functionality you need

Other frameworks There are a LOT of JavaScript frameworks to help or hinder your coding. Some of them need JQuery while others are standalone. Durandal, UnderscoreJS, Knockout (excellent tutorials), RequireJS, BackboneJS to name a few. Angular JS(Google), Ember JS are the two of the most popular at the moment. They both feature MVVM-like functionality (The view (HTML) speaking to the Model (our code) and vice versa). HTML example <input ng-model= age /> Can in angular be used directly in the code as it has the same value as: $scope.age

The end I hope you ve picked up a few pointers that will help you in your JavaScript transition. Thank you for your time. Andreas Ahlgren aa@computas.com