I just met you, and 'this' is crazy, but here's my NaN, so call(me) maybe? JavaScript is so weird.

Slides:



Advertisements
Similar presentations
The JavaScript Programming Language
Advertisements

Introduction to Programming
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Today Programming Style and Your Brain And Then There Was JavaScript Function the Ultimate The Metamorphosis of Ajax ES5: The New Parts.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Conditional Statements and Loops. Today’s Learning Goals … Learn about conditional statements and their structure Learn about loops and their structure.
JavaScript Language Fundamentals. About JavaScript JavaScript is not Java, or even related to Java –The original name for JavaScript was “LiveScript”
Compilation Encapsulation Or: Why Every Component Should Just Do Its Damn Job.
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.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
18-Jun-15 JavaScript Language Fundamentals. 2 About JavaScript JavaScript is not Java, or even related to Java The original name for JavaScript was “LiveScript”
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Taking JavaScript Seriously IS NOT THE WORST IDEA.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Data Types.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
CSE 341 Lecture 29 a JavaScript, the bad parts slides created by Marty Stepp see also: JavaScript: The Good Parts, by.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Programming 101 EPGY Middle School Programming Adam Leeper.
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Introduction to Web Frontend Development with JavaScript.
Working with Loops, Conditional Statements, and Arrays.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 How did your team meetings go? A list of three possible “adoptable” projects is linked near the bottom.
GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
JavaScript, Sixth Edition
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
JavaScript Introduction
CHAPTER 10 JAVA SCRIPT.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Selection (also known as Branching) Jumail Bin Taliba by
The switch Statement, and Introduction to Looping
JavaScript: Odds & Ends
Scope, Objects, Strings, Numbers
Week#2 Day#1 Java Script Course
What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language (a scripting language is a lightweight.
University of Kurdistan
JavaScript Reserved Words
By Hector M Lugo-Cordero September 3, 2008
Lisp.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

I just met you, and 'this' is crazy, but here's my NaN, so call(me) maybe? JavaScript is so weird

What makes JavaScript fun? (and by fun I mean....a little bit strange) Eval parseInt NaN With JSLint/JSHint Arrays Switches more... JavaScript Blocks Functions Null Equality Truthy/Falsey Objects this!

JavaScript...is not Java Java is to JavaScript as car is to carpet Java is to JavaScript as ham is to hamster

Syntax Error Automatic semicolon silliness {} ;

Syntax Error Most often, a newline (\n) ends a statement unless... The statement has an unclosed parenthesis ")", array literal, or object literal. The line uses -- or ++ Any block statements such as for, while, do, if, or else, and there is no starting bracket "{" After these constructs: break, continue, return, throw

Just say no to single line blocks One single line if (ok) x = true; Then looks like (to the dev) if (ok) x = true; callFunc(); Devs turn into if (ok) x = true; callFunc(); In reality it is (to the parser) if (ok) { x = true; } callFunc();

Putting the "fun" in functions function functionStatement() { // Some code return value; } var functionExpression = function() { // Some code return value; };

Putting the "fun" in functions (function IIFE() { console.log("function expression"); })(); (function () { console.log("anonymous function"); })();

Putting the "fun" in functions function main() { functionStatement(); function functionStatement() {... } // oh noes! this won't work functionExpression(); var functionExpression = function() {... } }

DEMO Fun with functions

Arrays There is no such thing No dimensions No out of bounds errors typeof doesn't know the difference

Carry on continue statement "I've never seen a piece of code that was not improved by removing it" -- Crockford

The switch Auto fallthroughswitch (expression) { case expression: statements [break;] case expression: statements [break;] default: statements [break;] }

Truthy values 'false' (quoted false) '0' (quoted zero) () (empty functions) [] (empty arrays) {} (empty objects) All other values Falsey values false 0 (zero) '' (empty string) null undefined NaN Let's get to the truth of the matter

false 0 NaN Null "" undefined "0" [] {} "false" Function

===== All things being equal-ish. Maybe. !=!==

DEMO Truthy and Falsey

Just what's up with this, anyway?

Or just a lowly, misunderstood, function? Eval - the most evil of all? var x = eval("forms[0].option" + optionNumber + ".checked"); var x = forms[0]["option" + optionNumber].checked;

Are you with me, or against me? with (objX) { // statements }

New, new, don't do typed wrappers new object new array new Date

farceInt(fib); static int parseInt(String s) static int parseInt(String s, int radix)

parseInt's farce parsing var result = parseInt(numericString, 10); 0X or 0x16 (hexadecimal) 010 (decimal) Everything else10 (decimal) 8 (octal)

NaN It claims it's NotANumber, but it's a number. Don't use equality operators with NaN Use Number.isNaN() instead Use typeof instead Don't use plain isNaN() – or else nothing is a number! ES6 Number.isNaN()

Seems legit var add = function() { return arguments[0] + arguments[1]; }; console.log(add(4, 4)); // returns 8 Is legit var add = function(where are the arguments?) { return arguments[0] + arguments[1]; }; console.log(add(4, 4)); // returns 8

How to avoid all the odd stuff JSLint JSHint JSFiddle