More on JavaScript Functions

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

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

Instructor s Notes Web Programming JavaScript Functions. Web Programming JavaScript Functions

A.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies

To implement binary trees, we have two prototypes: BTNode and BinaryTree.

JavaScript: Sort of a Big Deal,

JavaScript CS 4640 Programming Languages for Web Applications

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Functional Programming. Pure Functional Programming

CSC Web Programming. Introduction to JavaScript

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

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

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

CHIL CSS HTML Integrated Language

The JavaScript Language

Fundamental Concepts and Definitions

JavaScript CS 4640 Programming Languages for Web Applications

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

Introduction to Programming Using Java (98-388)

Working with JavaScript

Node.js Training JavaScript. Richard richardrodger.com

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

Notes on JavaScript (aka ECMAScript) and the DOM

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Ruby: Introduction, Basics

JavaScript: Features, Trends, and Static Analysis

FALL 2017 CS 498RK JAVASCRIPT. Fashionable and Functional!

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Short Notes of CS201

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Decaf Language Reference Manual

CS201 - Introduction to Programming Glossary By

A Short Summary of Javali

Scheme in Scheme: The Metacircular Evaluator Eval and Apply


COMP519 Web Programming Lecture 14: JavaScript (Part 5) Handouts

CS177 Python Programming. Recitation 2 - Computing with Numbers

Coding in JavaScript functions

The Decaf Language. 1 Lexical considerations

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

The PCAT Programming Language Reference Manual

HTML5 and CSS3 More JavaScript Page 1

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

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

Chapter 3 Data Types and Variables

JavaScript II. Overview

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Web Site Development with HTML/JavaScrip

Lesson 9: Custom JavaScript Objects

FUNCTIONS. The Anatomy of a Function Definition. In its most basic form, a function definition looks like this: function square(x) { return x * x; }

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS201 Some Important Definitions

JavaScript: More Syntax

Typescript on LLVM Language Reference Manual

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

Ruby: Introduction, Basics

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

EXAM Microsoft MTA Software Development Fundamentals. Buy Full Product.

Petros: A Multi-purpose Text File Manipulation Language

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lambda Calculus LC-1

Functional Programming - 2. Higher Order Functions

Contents I Introduction 1 Introduction to PL/SQL iii

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

Ruby: Introduction, Basics

Introduction to Web Tech and Programming

Chapter 1. Fundamentals of Higher Order Programming

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

JavaScript for C# Programmers Kevin

Discussion 12 The MCE (solutions)

Functional programming

3 The Building Blocks: Data Types, Literals, and Variables

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Extending SystemVerilog Data Types to Nets

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

functional programming in Python, part 2

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

c122mar413.notebook March 06, 2013

Assignment 7: functions and closure conversion

Javascript. Many examples from Kyle Simpson: Scope and Closures

Assignment 7: functions and closure conversion (part 1)

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

Web Application Development

CS201- Introduction to Programming Current Quizzes

The SPL Programming Language Reference Manual

Chapter 6 Introduction to Defining Classes

CIS 339 Section 2. What is JavaScript

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

Fundamentals: Expressions and Assignment

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

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Some Facts Web 2.0/Ajax Security

Java+- Language Reference Manual

Transcription:

More on JavaScript Functions Nesting Function Definitions Function definitions can be nested. <html> <body> function hypotenuse(a, b) function square(x) return x * x; return Math.sqrt(square(a) + square(b)); document.writeln( hypotenuse(3, 4)); </body> </html> 5 JavaScript is statically (or lexically) scoped: A non-local variable in the current block is identified with the variable with that name in the lowest block where the variable is declared that encloses the current block. 115

Functions as Data Functions are objects they re data that can be assigned to variables. function square(x) return x * x; var sqr = square; document.writeln( sqr(4)); 16 We can define a function with the Function() constructor. It expects any number of string arguments. The last is the body of the function. The rest specify the arguments of the function. var f = new Function( x, y, return x * y; ); Somewhat similarly, predefined function eval takes a string argument and evaluates it. For example, if x is 3 and y is 2, then eval( x + y ) evaluates to 5. 116

A function literal (defining an anonymous function) is like a function statement but is used as an expression and no funciton name is specified. var f2 = function(x, y) return x * y; ; The advantage of the Function() constructor is that we can construct the body of the function at run-time using the usual ways to manipulate strings. var f1 = new Function("x", "y", "return x * y;"), f2 = function(x, y) return x * y; ; document.writeln( f1(3, 4), " ", f2(2, 5) ); 12 10 117

This shows the things that can be done when functions are used as data. It shows that functions can be passed as arguments to other functions. And it shows how functions can be stored in an associative array (explained later). An array is an object, which inherits from the Object prototype (the highest prototypical object in the inheritance hierarchy of objects). All objects (not just arrays) are passed by reference (although this isn t illustrated here). 118

function add(x, y) return x + y; function subtract(x, y) return x - y; function multiply(x, y) return x * y; function divide(x, y) return x / y; function operate(operator, operand1, operand2) return operator(operand1, operand2); document.writeln(operate(add, operate(subtract, 10, 5), operate(multiply, 4, 5)), "<BR>"); operators = new Object(); operators["add"] = function(x, y) return x + y; ; operators["subtract"] = function(x, y) return x - y; ; operators["multiply"] = function(x, y) return x * y; ; operators["divide"] = function(x,y) return x/y; ; operators["pow"] = Math.pow; function operate2(op_name, operand1, operand2) if (operators[op_name] == null) return "unknown operator"; else return operators[op_name](operand1, operand2); document.writeln(operate2("add", "hello", operate2("add", " ", "world")), "<br>", operate2("pow", 10, 2) ); 25 hello world 100 119

The arguments Object Although a JavaScript function is defined with a fixed number of named arguments, it can be passed any number of arguments when it is invoked. The arguments[] array (actually an object and not an array) gives full access to these argument values, even when some are unnamed. Given a call f(v 0, v 1, ), in the definition of function f, arguments[0] is v 0, arguments[1] is v 1, etc. arguments.length is the number of arguments in the call. JavaScript doesn t check that a function is invoked with the correct number of arguments. But we can use the arguments object to do this. function f(x, y, z) if ( arguments.length!= 3 ) alert( function f called with + arguments.length + arguments, but it expects 3. ); return null; // The normal code goes here. 120

We can also exploit arguments[] to write functions that take variable numbers of arguments. function max() var m = Number.NEGATIVE_INFINITY; for ( var i = 0; i < arguments.length; i ++ ) if ( arguments[i] > m ) m = arguments[i]; return m; document.writeln( max( 1, 10, 100, 2, 3, 1000, 4, 5, 10000, 6 ) ); 10000 121