Martin Kruliš 2. 4. 2015 by Martin Kruliš (v1.0)1.

Slides:



Advertisements
Similar presentations
Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
Advertisements

A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Martin Kruliš by Martin Kruliš (v1.0)1.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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.
CSE 190M Extra Session #5 JavaScript scope and closures slides created by Marty Stepp
Taking JavaScript Seriously IS NOT THE WORST IDEA.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Topics Scope Scope and Lifetime Referencing Environments.
Martin Kruliš This is an Object Oriented system. If we change something, the users object by Martin Kruliš (v1.0)1.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JavaScript, Fourth Edition
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Advanced topic - variable.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
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.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
Martin Kruliš by Martin Kruliš (v1.0)1.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Objects.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
JavaScript scope and closures
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
OOP Basics Classes & Methods (c) IDMS/SQL News
Martin Kruliš by Martin Kruliš (v1.0)1.
Click to add Text Javascript Presented by Bunlong VAN Basic.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Martin Kruliš by Martin Kruliš (v1.1)1.
Lambda Functions & Closures A Sydney PHP Group Presentation 2 nd October 2008 By Timothy Chandler.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Chapter 4 Client-Side Programming: the JavaScript Language
Programming with ANSI C ++
Java Primer 1: Types, Classes and Operators
JavaScript Martin Kruliš by Martin Kruliš (v1.2)
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Modern JavaScript Develop And Design
Scope, Objects, Strings, Numbers
JavaScript Syntax and Semantics
JavaScript.
JavaScript an introduction.
JavaScript Objects
Introduction to Classes
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript in More Detail
CS5220 Advanced Topics in Web Programming JavaScript Basics
Object-Oriented Programming in PHP
CS3220 Web and Internet Programming JavaScript Basics
JavaScript CS 4640 Programming Languages for Web Applications
Java Programming Language
CS3220 Web and Internet Programming JavaScript Basics
JavaScript Internals and OOP Concepts
Presentation transcript:

Martin Kruliš by Martin Kruliš (v1.0)1

 Values ◦ Any expression or literal produces a value ◦ There are following value types: number, string, boolean, object, function, and undefined  Operator typeof returns the type of an expression ◦ Values are automatically garbage-collected when no longer needed ◦ Some type conversions are performed automatically  "5" + 4 // is "54"  "5" * 4 // is 20  console.log(myObject) // converted to string by Martin Kruliš (v1.0)2

 Variables ◦ Mnemonic holders for values  Rather “attachments” to values than “memory boxes” ◦ Declared by var keyword var x; var y = 1; var a, b, c; ◦ The declaration is made in the current scope  In global scope, the variables are assigned to the script environment (e.g., object window in the browser) var x = 1; and window.x = 1; are equivalent  In a function, the variable belongs to the local scope (more details later) by Martin Kruliš (v1.0)3

 Objects ◦ Objects are unordered name-value collections ◦ All members are public var myObject = { foo: 10, bar: function() {... } }; myObject.bar(); myObject.anotherFoo = 100; by Martin Kruliš (v1.0)4 Creates simple object with two members (foo and bar), where foo is a Number and bar is Function (i.e., in some sense a method). Members may be added dynamically.

 Prototypes ◦ Every object may have a prototype object ◦ Prototypes substitute classes in a specific way by Martin Kruliš (v1.0)5 myObject.printName(); [[Prototype]] foo bar [[Prototype]] foo bar [[Prototype]] name printName() [[Prototype]] name printName() Object.prototype AnotherObjectmyObject Searches up the prototype chain, looks for the first printName property myObject.name = …; Creates new member of myObject

 Constructors var Circle = function(r) { this.radius = r; }; Circle.prototype.foo = function() { … } var myCircle = new Circle(42); by Martin Kruliš (v1.0)6 Constructor looks like an ordinary function this refers to the newly created object (so it can be initialized) The prototype attribute is set to an empty object … so it can be easily augmented Creates new object and copies Circle.prototype to internal [[Prototype]] of the new object

 Constructors Example by Martin Kruliš (v1.0)7 myObj MySubclass MyClass.prototype MyClass.prototype MySubclass.prototype MySubclass.prototype myObj [[Prototype]] myObj [[Prototype]] empty object [[Prototype]] empty object [[Prototype]] MyClass obj [[Prototype]] MyClass obj [[Prototype]] new Object.prototype Class-based Language JavaScript Indirect prototyping Inheritance Instatiation

 Functions ◦ Special “callable” objects (first-class functions) ◦ Various ways to create them function foo(args) { body } var foo = function(args) { body } var foo = new Function(args, "body"); ◦ Function declaration = object creation ◦ Variadic – implicitly variable arity ◦ No difference between functions and methods by Martin Kruliš (v1.0)8 Slight difference: declarations (1) are processed at parsing time, expressions (2) are processed at runtime.

 Function Scope of Variables var a1; // global scope (obj. window in Browser) function foo() { var a2; // local scope of foo() function innerFoo() { var a3; // local scope of innerFoo() function innerInnerFoo() { // I can see a1, a2, and a3 from here … a2 = 1; } by Martin Kruliš (v1.0)9 We follow the scope chain upwards and find the first variable of the name ' a2 '.

 Execution Context ◦ All JS code is executed in execution context  Abstraction ~ object that holds variables  Global context is represented by the global object  E.g., window object in the browser ◦ New context is created when function is called  Function calls shape the scope chain of exec. context 1.An activation object is created 2.Calling arguments are set in the activation object 3.Scope chain is selected from the [[Scope]] property of the function object (and becomes execution context) 4.Activation object is pushed at the front of scope chain by Martin Kruliš (v1.0)10

 Scope Chain ◦ Similar to prototype chain, but…  Both reads and writes follow the chain  New (local) variables are created by var keyword var a = 1; function foo(x) { a = 2; var b = 3; c = 3; } foo(3); by Martin Kruliš (v1.0)11 a foo() c a foo() c foo [[Scope]] (global context) xbxb xbxb activation object

 Closure ◦ Created functions capture current execution context  Into internal [[Scope]] property ◦ Some parts of the scope chain may become inaccessible from the current execution context  By defining nested functions and returning them out of their creation scope  The hidden part of the scope chain is still accessible from the created (nested) function ◦ The isolated scope chain is usually called closure by Martin Kruliš (v1.0)12

 Closure function make() { var x = 1; return function() { alert(x++); } } var f1 = make(); var f2 = make(); f1(); f2(); by Martin Kruliš (v1.0)13 make() f1() f2() make() f1() f2() global context [[Scope]] f1 function [[Scope]] f2 function x = 1 make() act. obj x = 1 make() act. obj

 Closure Application function createAdder(x) { return function(y) { return x + y; } var add3 = createAdder(3); var add7 = createAdder(7); add3(10); // is 13 add7(10); // is by Martin Kruliš (v1.0)14 When the inner function is created, the closure captures value of x == 3 New function have a new closure where x == 7 The Inner function can see variable x due to scoping rules

 Closure Pitfalls function createLinks() { for (var i = 1; i <= 5; ++i) { var link = document.createElement('a'); link.onclick = function(){ alert(i); }; link.textContent = "Link " + i; document.body.appendChild(link); } document.onload = createLinks; by Martin Kruliš (v1.0)15 All the links are created in one scope, thus sharing one closure. The value of i is 5, when the scope is closed. Always prints “5”.

 Pushing Object in Scope Chain ◦ with (obj) statement ◦ Given object is pushed at the top of the chain var o = { x: 1 }; var foo; with (o) { foo = function() { alert(x++); } by Martin Kruliš (v1.0)16 Members of object o become part of the variable scope. x is accessible here

by Martin Kruliš (v1.0)17