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

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

Intro To Javascript. Intro to Web Development

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

welcome to BOILERCAMP HOW TO WEB DEV

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

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

SEEM4570 System Design and Implementation Lecture 03 JavaScript

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

CGS 3066: Spring 2015 JavaScript Reference

Client Side JavaScript and AJAX

JavaScript for C# Programmers Kevin

JavaScript for PHP Developers

Functions. Functions in JavaScript are declared using the function keyword

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

Web Application Development

Welcome to CS50 section! This is Week 10 :(

Princess Nourah bint Abdulrahman University. Computer Sciences Department

JAVASCRIPT - CREATING A TOC

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

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

a Very Short Introduction to AngularJS

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

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

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

JavaScript Introduction

AngularJS Intro Homework

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

JavaScript: Sort of a Big Deal,

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!

Week - 01 Lecture - 04 Downloading and installing Python

Comprehensive AngularJS Programming (5 Days)

INTRODUCTION TO JAVASCRIPT

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

STARCOUNTER. Technical Overview

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

JavaScript. The Bad Parts. Patrick Behr

CS558 Programming Languages

JavaScript. What s wrong with JavaScript?

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

BM214E Object Oriented Programming Lecture 8

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

Quick.JS Documentation

CS 130(0) JavaScript Lab

JAVASCRIPT FOR THE C# DEVELOPER

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

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

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

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

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

Client-Side Web Technologies. JavaScript Part I

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

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

Tarek Elachkar, VP Customer Solutions - Jahia A real world story of Angular and Apache Unomi integration.

JQuery and Javascript

JavaScript: The Basics

(Refer Slide Time: 01:40)

JavaScript Lecture 1

INF5750. Introduction to JavaScript and Node.js

jquery CS 380: Web Programming

Notes on JavaScript (aka ECMAScript) and the DOM

Ajax HTML5 Cookies. Sessions 1A and 1B

COMP-520 GoLite Tutorial

Max and Programming Is Max a Programming Language?

An Introduction to TypeScript. Personal Info

Web API Lab. The next two deliverables you shall write yourself.

Finding and Fixing Bugs

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Anatomy of a SPA: Client-side MVC

Getting Started with

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

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

IN4MATX 133: User Interface Software

3 Days Training Program

LEARN WITH INTRODUCTION TO TYPESCRIPT

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

JavaScript: Getting Started

Part 1: jquery & History of DOM Scripting

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

CS558 Programming Languages

JavaScript Basics. The Big Picture

this is a cat CS50 Quiz 1 Review

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

JavaScript Basics. Mendel Rosenblum. CS142 Lecture Notes - JavaScript Basics

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Developer Entrepreneur Crazy person

JavaScript Functions, Objects and Array

Data Visualization (DSC 530/CIS )

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

LAST WEEK ON IO LAB. Install Firebug and Greasemonkey. Complete the online skills assessment. Join the mailing list.

CL_55244 JavaScript for Developers

Client vs Server Scripting

Chapter 1 Getting Started

Your first C++ program

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Transcription:

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

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 Very powerful and fast Easy to learn* *except if you come from a Java or C# background

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

The var 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 a = 1; // number a = {}; // object a = hello ; // 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) Everything is in global scope by default! var a = Im a global ; // defined in global scope Scope in JavaScript is defined by... functions!

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 // true undefined == null // true undefined == // false null == 0 // 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 fucntion 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); }

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

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, common mistake var myobject = { firstname:"john", lastname: "Doe", fullname: function () { return this.firstname + " " + this.lastname; } } myobject.fullname(); // this points to the object

Functions (as classes) function MyHero(name, role) { this.name = name; this.role = role; this.attack = function (target) { alert("attacking " + target); } } MyHero.roshanKills = 2; // add properties on the fly var hero = new MyHero("Luna", "Carry"); hero.attack("tinker"); console.log (hero.roshankills); // 2

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());

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].innertext = my own text ; for (var i = 0; i < = nodes.length; i++ ) { nodes[i].innertext = 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 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