JavaScript: Sort of a Big Deal,

Similar documents
JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

CSC Web Programming. Introduction to JavaScript

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

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

Client-Side Web Technologies. JavaScript Part I

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

Symbols. accessor properties, attributes, creating, adding properties, 8 anonymous functions, 20, 80

Introduction to Programming Using Java (98-388)

JavaScript for C# Programmers Kevin

INF5750. Introduction to JavaScript and Node.js

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Java: introduction to object-oriented features

CHAPTER 7 OBJECTS AND CLASSES

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

9 Working with the Java Class Library

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Java Primer 1: Types, Classes and Operators

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Lecture 16: Static Semantics Overview 1

Pace University. Fundamental Concepts of CS121 1

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CHAPTER 7 OBJECTS AND CLASSES

Fundamental Concepts and Definitions

CMSC 330: Organization of Programming Languages

Boot Camp JavaScript Sioux, March 31, 2011

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Chapter 6 Introduction to Defining Classes

(Not Quite) Minijava

Object Model. Object Oriented Programming Spring 2015

JavaScript Lecture 1

Syntax Errors; Static Semantics

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Functional JavaScript. Douglas Crockford Yahoo! Inc.

Web Application Development

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Static Analysis for JavaScript

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

A Short Summary of Javali

CSE 341, Autumn 2015, Ruby Introduction Summary

Node.js Training JavaScript. Richard richardrodger.com

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

JavaScript: The Good Parts

Principles of Programming Languages

The SPL Programming Language Reference Manual

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

COMP-520 GoLite Tutorial

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

JavaScript Lecture 3b (Some language features)

System Software Assignment 1 Runtime Support for Procedures

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Decaf Language Reference

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Semantic Analysis and Type Checking

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

iwiki Documentation Release 1.0 jch

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

This document defines the ActionScript 3.0 language, which is designed to be forward- compatible with the next edition of ECMAScript (ECMA-262).

Know Your Engines How to Make Your JavaScript Fast

Generating Continuation Passing Style Code for the Co-op Language

Chapter 3 Data Types and Variables

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

G Programming Languages - Fall 2012

Timing for Interfaces and Abstract Classes

Overview. Elements of Programming Languages. Objects. Self-Reference

Variable Declarations

What is Inheritance?

Another IS-A Relationship

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

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Decaf Language Reference Manual

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Scala : an LLVM-targeted Scala compiler

Classes vs Simple Object Delegation In JavaScript

IT441. Subroutines. (a.k.a., Functions, Methods, etc.) DRAFT. Network Services Administration

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

CS558 Programming Languages

Principles of Programming Languages

CMSC 132: Object-Oriented Programming II

Type Bindings. Static Type Binding

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Object Model. Object Oriented Programming Winter

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

EMBEDDED SYSTEMS PROGRAMMING More About Languages

CS260 Intro to Java & Android 03.Java Language Basics

egrapher Language Reference Manual

Weeks 6&7: Procedures and Parameter Passing

INF3110 Programming Languages Runtime Organization part II

INF3110 Programming Languages Runtime Organization part II

JavaScript. What s wrong with JavaScript?

JavaScript: The Good Parts. Douglas Crockford Yahoo! Inc.

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Transcription:

: Sort of a Big Deal, But Sort of Quirky... March 20, 2017

Lisp in C s Clothing (Crockford, 2001) Dynamically Typed: no static type annotations or type checks. C-Like Syntax: curly-braces, for, semicolons, dot operator. Object-Oriented: OOP patterns, properties, inheritance (prototypal, not class-based). Functional: FP patterns, function literals, closures with lexical scoping.

Programming languages that influenced (Rauschmayer, 2014)

Problem and Goal Problem: Though JS may syntactically look similar to other languages, many may find the language s semantics surprising. Goal: Present a few of these to promote awareness.

A Surprising Example What is going on here? function f() { x = new Object (); f() window. x === x && x === this. x // true?! x is a variable, but somehow we can access it in 3 syntactically distinct ways. Here, we can see that window, the unique global object, is: An object by which variables are accessed. An object by which properties are accessed. An object produced by the this keyword.

Values and Types

Values in Dynamically Typed: All type info is at runtime; variables have no static type annotations. Weakly Typed: Few type checks; lots of implicit type coercion. Functions: Functions are values that can be instantiated dynamically and treated as values. Objects: Objects have properties which store values; when a property lookup is called, it acts as a method. Objects Are Just Maps: Properties can be dynamically added, removed, and assigned values. No UDTs: No language support for static user-defined types (UDTs), but they can be emulated via constructors which manipulate object properties.

Some Familiar Literals String Literal: "Foo" Number Literal: 42

Array Literals var arr = [" Hello ", " world ", "!", 42];

Object Literals var obj = { foo : " bar ", n: 42 ;

Function Literals Aside from their names, these two functions are equivalent in every way. The former is syntactic sugar for the latter. function f( x) { return x + 42; ; var g = function ( x) { return x + 42; ;

Nested Function Literals var enclosed = 42; function outer () { function inner () { return enclosed ; return inner ; enclosed === outer () (); // == > true We ll talk more about scopes later.

Functions and Arrays are actually objects. ES6 defines seven datatypes: Boolean Null Undefined Number String Symbol (new in ES6) Object

These types don t quite match typeof ES6 defines the following behavior for the typeof operator: typeof Undefined ==> "undefined" typeof Null ==> "object" typeof Boolean ==> "boolean" typeof Number ==> "number" typeof String ==> "string" typeof Symbol ==> "symbol" typeof Impl-dependent Function Object ==> "function" typeof Object ==> "object"

Values and Types What is this? Variables and Scopes Inheriting Properties References Weak Typing in https://www.destroyallsoftware.com/talks/wat

What is this?

What is this? w3schools: In, the thing called this, is the object that owns the current code. this is a keyword which evaluates to the current invocation context, and how it is set depends upon how the current function was invoked.

this Is Set In One Of Four Ways Method Invocation: If the current fuction was invoked as a method, then this is the receiver object. Constructor Invocation: If the current function was invoked as a constructor (i.e. using the new keyword), then this is the new object being constructed. Explicit Argument: If the current function was called via its call() or apply() methods, then this will be the first argument passed in. Otherwise: If in the root of the script or in a normal function invocation, this is the global object.

Variables and Scopes

Why does this happen? function outer () { function inner () { x = 5 inner (); outer (); console. log (x); // == > 5

C, C++, and Java Variables Are Block Scoped public void f() { for ( int i = 0; i <10; i ++) { System. out. println (i); // Cannot use i here.

Variables are Not Block Scoped. They are function scoped. function f() { for ( var i = 0; i <10; i ++) { console. log (i); // Can use i here. console. log (i) // == > 10

Variable Declarations are Hoisted function f() { // i is declared but uninitialized. console. log ( i) // == > undefined var i = 42 console. log (i) // == > 42

Variable Lookups in a Nested Function? var x = new Object (); function outer () { function inner () { return x; return inner ; x === outer () (); // == > true The x variable which is read is the nearest variable with this name in the scope chain. The search goes: inner, outer, then finally window (the global object).

Variable Assignments in a Nested Function? function outer () { function inner () { x = 5 inner (); outer (); console. log (x); // == > 5 If no x variable is found in searching the scope chaing, then this implicitly declares a variable x as a property of the global object and initializes it. 1 1 As of ES5, one can prevent this foolishness by using strict mode.

How Is the Scope Chain Determined? For some user-defined function f, the parent scope of f is the scope in which f was instantiated. function wrap ( wrapped ) { function wrapper () { return wrapped ; return wrapper ; var a = {a: "a"; var b = {b: "b"; var a_ wrapper = wrap ( a); var b_ wrapper = wrap ( b); a_ wrapper () === a; // == > true b_ wrapper () === b; // == > true

Our Surprising Example (Revisited) This example illustrates how the global object is at the intersection of these three different language features: variable, properties, and invocation contexts. function f() { x = new Object (); f() window. x === x && x === this. x // true! We cannot normally use property accesses on our scopes: we don t have expressions which evaluate to a scope. But there s one exception: the global object.

Inheriting Properties

Does Property Inheritance Differently Not class-based or classical inheritance. Prototypal inheritance.

Any Function Instance Can be Used as a Constructor var MyConstructor = function ( x) { this. myproperty = x; //... var obj = new MyConstructor (5) ; console. log ( obj ); // == > MyConstructor { myproperty : 5

Every Constructor Instance (i.e. Function Instance) has a prototype Property An object o constructed by some constructor C will inherit properties from C.prototype via a hidden property o. proto. We can manually set C.prototype. By default, JS initializes the protoype to an empty object instance.

There are Actually Three Ways to Designate an Object s Prototype Constructor Functions: Set a constructor function s prototype property, and this object will become the prototype of all object instanted from this constructor. (We saw this one.) Explictly at Object Instantiation: The Object.create() helper function creates a new object instance with a given prototype. Set Manually: Object.setPrototypeOf()

Prototype Chain Search Reading From obj.p: The prototype chain of obj is searched, looking for the first object which defines p. If p is never found, undefined is returned. Assigning To obj.p: The prototype chain of obj is searched, looking for the first object which defines p. If p is never found, p is defined directly on obj.

Scope Chains vs Prototype Chains Scope Chain: Extended by function call. Extends from function definition scope. (Lexical/static scoping) Stored values accessed via variable accesses. The chain s root is always the global object. Prototype Chain: Extended by object creation. Extends from a designated object. Stored values accessed via property accesses. The chain s root is always null.

Crockford, D. (2001). Javascript: The world s most misunderstood programming language. Rauschmayer, A. (2014). Speaking : An In-Depth Guide for Programmers. O Reilly Media, Inc..