Julian on JavaScript: Functions Julian M Bucknall, CTO.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Julian on JavaScript: Objects Julian M Bucknall, CTO.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Introduction to JavaScript Gordon Tian
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
ECE122 Feb. 22, Any question on Vehicle sample code?
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Function the Ultimate Act III. Function Method Class Constructor Module.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
JavaScript scope and closures
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
JavaScript for C# developers Dhananjay Microsoft MVP
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Getting from Scripts to Applications Donald J. Sipe | Refresh Jacksonville | November 11 th 2008.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Click to add Text Javascript Presented by Bunlong VAN Basic.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
JavaScript Patterns to Clean Up Your Code Dan Wahlin.
By: Travis Powell and Chad Herman. About CoffeeScript The Golden Rule: o "Its just JavaScript" One-to-One Compilage into equivalent JavaScript Passes.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
Variable Scope Variable, variable, who’s got the variable?
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
JavaScript and Ajax (Expression and Operators)
JavaScript Miscellany
CS212: Object Oriented Analysis and Design
Chapter 9 Objects and Classes
Nate Brunelle Today: Functions again, Scope
CS5220 Advanced Topics in Web Programming JavaScript Basics
ECE 103 Engineering Programming Chapter 12 More C Statements
Chapter 6 Objects and Classes
Classes, Objects and Methods
CS3220 Web and Internet Programming JavaScript Basics
Scope Rules.
Presentation transcript:

Julian on JavaScript: Functions Julian M Bucknall, CTO

Functions are objects −Is an encapsulation of some code −Can have properties and methods of it’s own −Can appear as a parameter, can be returned from another function −Like all objects, functions are passed by reference not value

Defining a function −Two main ways: −Function literal −var f = function(arguments) { … }; −var f = function optName(arguments) { … }; −Function statement −function f(arguments) { … }

Return value? −All functions return something −There is no void type −“return;” will return undefined −If no return statement −Function returns undefined −Unless it’s a constructor, called via new

Invocation −When invoked, all functions get two extra variables −this − −arguments −The parameter values of the call as an array-like object

Invocation patterns −Method −Function −Constructor −“apply”

Method invocation −Defined as a method on an object −Called via that object − this points to the object containing the method −Like C#, really

Function invocation −Defined as a global function −Or, defined as an internal function −Called without reference to an object −this points to the Global Object −Catches everyone out

The Global Object −Has no name −Is where all unowned variables (primitives, objects, functions) end up as public properties −are visible to all code! −Browser sets up a special property called window to point to the Global Object −(window is a property of the Global Object)

Constructor invocation −Defined as normal function −Convention: Name has initial uppercase letter −Called with new keyword −this points to object being constructed −constructor property already set −Object constructed is returned by default −No need for return statement

“apply” invocation −Defined however you want −Called via the function’s apply method −Alternately: use the call method −You get to define the this object

Scope −Scope in JavaScript is by function −NOT braces as in C# −No block scope here −The this object stays with the outer function, inner functions get their own this (usually the Global Object) −Watch out for callbacks

Scope 2 −A variable declared in a function is visible throughout the function −Even before it’s lexically defined

Closures −The “yay!” to scope’s “ow!”

Thank You Julian M Bucknall ∙ CTO ∙