JavaScript - University Of California, San Diego

3y ago
11 Views
2 Downloads
1.78 MB
55 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Farrah Jaffe
Transcription

JavaScriptDeian Stefan(adopted from my & Edward Yang’s CSE242 slides)

Why JavaScript? Linga franca of the Internet Used in the browsers, used server-side, used for IoT Still evolving to address growing needs (EcmaScript) Interesting goals and design trade-offs Illustrates many core concepts of CSE 130

The great ideas [JavaScript]Expressive power (say more with less)First-class functionsType inferenceMonadsPattern matchingException handlingContinuationsReliability and reuseType polymorphismModulesType classesObjects & inheritanceCross-cutting concernsMemory managementConcurrency

The great ideas [Haskell]Expressive power (say more with less)First-class functionsType inferenceMonadsPattern matchingException handlingContinuationsReliability and reuseType polymorphismModulesType classesObjects & inheritanceCross-cutting concernsMemory managementConcurrency

The great ideas [C ]Expressive power (say more with less)First-class functionsType inferenceMonadsPattern matchingException handlingContinuationsReliability and reuseType polymorphismModulesType classesObjects & inheritanceCross-cutting concernsMemory managementConcurrency

Today A little bit of history Concepts from JavaScript First-class functions Objects Language flexibility

May 1995

May 1995We need a scripting languagefor the browser!

May 1995We need a scripting languagefor the browser!Can I use Scheme?

May 1995We need a scripting languagefor the browser!Can I use Scheme?Ha? No! Make it look like Java!

One week later

One week later Here is a hacked up prototype!

One week later Here is a hacked up prototype!Great! Let’s ship it!

One week later Here is a hacked up prototype!Great! Let’s ship it!(It really took another year to embed it in the browser)

JavaScript’s design goals [Eich, ICFP 2005] Make it easy to copy/paste snippets of code Tolerate “minor” errors — e.g., missing semicolons Simplify event handling (inspired by HyperCard) Pick a few hard-working, powerful primitives First-class functions (based off Scheme/Lisp) Objects everywhere (based off Self/Smalltalk)Leave all else out!

JavaScript has evolved EcmaScript 5 and 6 introduced many new features block scoping new types (Map, Set, Symbols, Uint8Array, etc.) strict mode module system classesHow could JavaScript have been useful without these?

First-class functions!

First-class functions What does it mean for a language to have first classfunctions? (functions are values)

First-class functions What does it mean for a language to have first classfunctions? (functions are values) can be declared within any scope

First-class functions What does it mean for a language to have first classfunctions? (functions are values) can be declared within any scope can be passed as arguments to a function

First-class functions What does it mean for a language to have first classfunctions? (functions are values) can be declared within any scope can be passed as arguments to a function can be returned as result of function call

Function as scoping primitive Today: JavaScript has block scoping But, until recently, JavaScript only had function-levelscoping What does this mean? How did people survive?

scope-*.js

Function as scoping primitive Whenever you want a new scope: declare a new function immediately call itKey requirement from language design: being able to declare function in any scope

Okay! But Why do we want to pass functions as arguments? Or return functions as results?

Functions as args Original reason: simple way to do event handling E.g., onclick(function() { alert(“button clicked!”); })Still true today. But many other reasons, including: performance: asynchronous callbacks expressiveness: filter, map-reduce, etc.

Performance? Don’t need to block when reading file Can tell runtime system to call your “callback” functiononce it’s read the file This allows runtime to schedule other IO concurrently

perf-*.js

Expressive power Say more with less! E.g., filter all positive elements from array E.g., add 42 to every element of the arrayIn both cases: we are expressing the computation wecare about without telling the computer what to do Don’t need to clutter code with low-levelmechanisms!Opens up room for performance optimizations! How?

expressive.js

Why return functions? With the other 2 properties: let’s you composefunctions from other functions Functions that do this are called “high-order”E.g., function composition: (f g)(x) f (g(x)) Here is a function that takes 2 functions: f and gE.g., instead of map(map(list, f), g) we can domap(list, g f): way faster!

hof.js

Aren’t these just function pointers?

No! JavaScript functions are closures! Closure function code environment Function pointers don’t keep track of environment We’ll see this in more detail in a few lectures

closure.js

What else can functions be used for? EcmaScript now has notion of modules But most implementations still use functionsHow can we use functions to implement modules? Closures are good for information hidingLocally declared variables are scoped to the function(“module”)Function called with exports object which is used toexpose public variables/functions

module*.js

Today A little bit of history Concepts from JavaScript First-class functions Objects Language flexibility

What are JavaScript Objects?

What are JavaScript Objects? Objects are maps of names (strings) to values

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation:

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation: e.g., const obj { x: 3, y: “w00t” }

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation: e.g., const obj { x: 3, y: “w00t” }Properties are accessed with dot or bracket notation:

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation: e.g., const obj { x: 3, y: “w00t” }Properties are accessed with dot or bracket notation: e.g., obj.x or obj[“x”]

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation: Properties are accessed with dot or bracket notation: e.g., const obj { x: 3, y: “w00t” }e.g., obj.x or obj[“x”]Methods are function-valued properties

What are JavaScript Objects? Objects are maps of names (strings) to values E.g., object created with object literal notation: Properties are accessed with dot or bracket notation: e.g., const obj { x: 3, y: “w00t” }e.g., obj.x or obj[“x”]Methods are function-valued properties e.g., obj.f function (y) { return this.x y; }

What is “this”? this is called the receiver Comes from Self (Smalltalk dialect) Will see more of this in objects lectureIntuitively: this points to the object which has thefunction as a method Really: this is bound when the function is called

receiver.js

I thought JavaScript had classes Now it does! But it didn’t always How did people program before? Used to use functions as constructors!

What is a function constructor? Just a function! When you call function with new the runtime bindsthe this keyword to newly created objectYou can set properties on the receiver to populateobjectOne property is special: prototype

class.js

Today A little bit of history Concepts from JavaScript First-class functions Objects Language flexibility

Language flexibility Does not require lines end in ‘;’ Casts implicitly to avoid “failures” Automatic ‘;’ insertion not always what you expectUseful in some case, usually source of errorsHoisting Sometimes useful, but, variable declarations (thoughnot definitions) are also hoisted

Language flexibility Evaluate string as code with eval Need access to full scope at point of call Scope depends on whether call is direct or notCan alter almost every object (“monkey patch”) Even built-in objects like window and fs What’s the problem with this?

Takeaways First-class functions are extremely powerful We’ll see this over and overLanguage “flexibility” is not free Think about features before shipping them

JavaScript’s design goals [Eich, ICFP 2005] Make it easy to copy/paste snippets of code Tolerate “minor” errors — e.g., missing semicolons Simplify event handling (inspired by HyperCard) Pick a few hard-working, powerful primitives First-class functions (based off Scheme/Lisp) Objects everywhere (based off Self/Smalltalk)

Related Documents:

JavaScript Manual for LCCS Teachers 13 Client-side JavaScript vs. server-side JavaScript For many years JavaScript was a client-side scripting language. This was because JavaScript programs could only be run from inside web browsers which were installed on client machines. Because of the fact that JavaScript code can run on client devices it means

- The Spark web app framework . Yahoo JavaScript PHP Amazon.com JavaScript Java, C , Perl Wikipedia.org JavaScript PHP, Hack Twitter.com JavaScript C , Java, Scala, Ruby Bing JavaScript ASP.net eBay.com JavaScript Java, JavaScript, Scala . Note the MVC architecture

JavaScript directly within the HTML file. We can also include more than one script tag and can include a mix of internal JavaScript and external JavaScript files. This will be useful, for example, if we have some JavaScript that is appropriate for many of our webpages and then some JavaScript that is specific to one page. Function Definition

JavaScript. Check a framework's documentation for details. Using the SDK with Web Browsers All major web browsers support execution of JavaScript. JavaScript code that is running in a web browser is often called client-side JavaScript. Using the SDK for JavaScript in a web browser differs from the way in which you use it for Node.js. The

Praise for Effective JavaScript "Living up to the expectation of an Effective Software Development Series pro-gramming book, Effective JavaScript by Dave Herman is a must-read for anyone who wants to do serious JavaScript programming. The book provides detailed explanations of the inner workings of JavaScript, which helps readers take better

HTML and CSS to provide interactive features on a web page. In this class, you will learn some basic JavaScript commands and build a to-do list web application using JavaScript to dynamically alter the appearance of the web page. Topics This class will cover the following concepts: 1. Introduction to JavaScript 2. JavaScript Basics 3. Using .

JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages JavaScript can put dynamic text into an HTML page - A JavaScript statement like this:

AWS SDK for JavaScript Developer Guide for SDK Version 3 Maintenance and support for SDK major versions What is the AWS SDK for JavaScript? Welcome to the AWS SDK for JavaScript Developer Guide. This guide provides general information about setting up and configuring the AWS SDK for JavaScript. It also walks you through examples and tutorial