SlideShare a Scribd company logo
LESSON THREE
Javascript and the DOM
Fast Review
Data Types in Javascript


•   (1) Numbers

•   (2) Strings

•   (3) Booleans

•   (4) Functions

•   (5) Objects

•   (6) Undefined
FUNCTIONS


The ultimate DRY

Encapsulate common functionality
function name( arg_1, ... arg_n ) {

    [ FUNCTION BODY ]

}
CONTROL FLOW

if
     •   Execute some code if a certain condition pertains

for
     •   Execute some code a “definite” number of times

while
     •   Execute some code an “indefinite” number of times
CONTROL FLOW: if

if ( condition_1) {
    [ DO SOME STUFF ]
} else if ( condition_2 ) {
    [ DO SOME OTHER STUFF ]
} else {
    [ DO DEFAULT STUFF ]
}
CONTROL FLOW: for



for ( var i = 0; i < 10; i++ ) {
    [ DO SOME STUFF ]
}
// That stuff will happen 10 times
CONTROL FLOW: while


while ( [SOME CONDITION OBTAINS] ) {
    [ KEEP DOING STUFF ]
}
// That stuff will keep happening until
// the condition is false
Where do we code
  our javascript?
1. Webkit Console
2. JS Fiddle
3. In-line javascript
4. Include a .js file
5. In Node.js Terminal
JAVASCRIPT
[more on functions]
FUNCTIONS AS ARGUMENTS
In Javascript, functions can be passed as
arguments to other functions
  •   Relatedly, they can be returned by functions and stored as
      variables

  •   This is a very special and valuable feature of the language

  •   One says that functions are “first class objects”

  •   Javascript, in this way, treats functions like data


var say_hi =         function() {console.log(“hi”);};
say_hi(); // Prints “hi”
BUZZZZWORD




    “First class objects”
•    These are the “objects” in an “object-oriented” language (we will define
     “object oriented” later) that can be (1) saved in variables, (2) passed as
     arguments to functions, and (3) returned as values from functions
ANONYMOUS FUNCTIONS


Javascript conveniently allows you to define
functions “inline” without specifying a name
var kill_timeout = set_timeout(
 function() {
     console.log(“You are now 5 seconds older”);
 }, 5000); // Javascript tells time in milliseconds
RECURSIVE FUNCTIONS
In Javascript, functions can call themselves
 •   This is called “recursion”

 •   It must “bottom out” at the “base case” or it will never end (and
     you will run out of memory!)


function factorial(n) {
  if ( n == 0 ) {
     return 1; // Factorial of zero is one by definition
  } else {
     return n * factorial( n - 1 ); // Recursive step
  }
}
JAVASCRIPT
[data structures]
ARRAYS




   Javascript
  [data structures]
ARRAYS
A data structure native to Javascript that
stores values in an ordered way
 •   Arrays contain an arbitrary number of “elements” which can be
     accessed individually by “indexing” into the array

 •   Defined by comma separating values between brackets

 •   Arrays are “indexed” from zero (not from one)


var team = [‘ben’,‘jeff’,‘kam’];
team[0] // => ‘ben’
team[2] // => ‘kam’
team[3] // => undefined
OBJECTS




    Javascript
   [data structures]
BUZZZZWORD



“Object Oriented”
•   A way of structuring programming languages invented in the early
    90s

•   Data and functions (“methods”) are grouped together in “objects”

•   The outside world can interact with these objects only through a
    rigidly defined interface

•   Javascript is centered on objects, but not object oriented in the
    traditional sense
OBJECTS


Objects are collections of “properties”
 •   Properties are key / value pairs

 •   Defined using braces

 •   Use “dot” notation to read and write

var person = {};

person.name = ‘will’; // Write

var name = person.name;                 // Read
OBJECTS


Can also use “javascript object notation” to
get and set properties
var person = {};

person[ ‘name’ ] = ‘will’; // Write

var name = person[ ‘name’ ];      // Read
OBJECTS


Everything in Javascript is an object
 •   Therefore everything can have properties

 •   Some values have “built in” properties

var name = ‘will’;

name.length; // => 4

var team = [‘ben’, ‘jeff’, ‘kam’];

team.length; // => 3
OBJECTS


Functions stored as properties are called
“methods”
var team = [‘ben’, ‘jeff’];

team.push( ‘kam’ ); // => team[2] == ‘kam’

// Push is an Array method that adds a value to the end of an array
“this”

“this” is used to refer to an object inside the
body of a method
var person = {};
person.name = ‘will’;
person.say_name = function() {
    console.log( this.name );
}
person.say_name(); // => ‘will’
THEORY VS. PRACTICE

There is a fair amount of theory underlying
object orientation
For our practical purposes, at the moment
you need only understand what properties
are, and how to set (write) and get (read)
them
var obj = {};
obj.prop = ‘some_val’; // Setting
var val = obj.prop; // Getting
Document Object Model (DOM)
   [ how javascript interacts with page content ]




                                             Introduction
Document Object Model


•   HTML documents have hierarchy

•   every element has a parent

•   every parent thus has children
Document Object Model




The tree is made up of nodes and text
DOM Traversal: Node Links




             x.childNodes             x.parentNode
             x.firstChild             x.lastChild
             x.nextSibling            x.previousSibling




not enough for ya? https://github.com/spicyj/jquery-genealogy
Problem: Not Sustainable



document.firstChild.lastChild.previousSibling.firstChild.nextSibling.style.display = “none”


                                           VS.


               document.getElementById(“dummy”).style.display = “none”;
BUZZZZWORD




    SUSTAINABLE
•    Sustainable code retains its integrity and function over time.

•    It is invulnerable to external influence!
DOM Traversal: Attribute Selection




var my_box = document.getElementById(“my-unique-id”);



var my_links = document.getElementsByTagName(“A”);
Important Properties

x.innerHTML

   document.innerHTML(“<p> oh no! </p>”);
x.style

   document.body.style.marginTop(“30px”);
x.getAttribute(name)

   my_link.getAttribute(“href”); // => “http://www.hackyale.com”
x.setAttribute(name, value)

   my_link.setAttribute(“href”, “http://www.20b.org/”);
The ‘document’ Object



The document is a special object, with it’s
methods and properties.
PUTTING IT ALL TOGETHER



Let’s put cats on stuff
QUESTIONS?
contact will_and_bay@hackyale.com

More Related Content

What's hot

Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
军 沈
 
javascript
javascript javascript
javascript Kaya Ota
 
J query1
J query1J query1
J query1
Manav Prasad
 
J query
J queryJ query
J query
Manav Prasad
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
Mindfire Solutions
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XML
Mohammad Shaker
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritancepedro.carvalho
 
Designing a JavaFX Mobile application
Designing a JavaFX Mobile applicationDesigning a JavaFX Mobile application
Designing a JavaFX Mobile applicationFabrizio Giudici
 
Javascript Journey
Javascript JourneyJavascript Journey
Javascript Journey
Wanqiang Ji
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
Mats Bryntse
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
Shlomi Komemi
 
javascript objects
javascript objectsjavascript objects
javascript objects
Vijay Kalyan
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
CSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+ReflectionCSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+Reflection
Mohammad Shaker
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Rangana Sampath
 
Java script
Java scriptJava script
Java script
Prarthan P
 

What's hot (20)

Prototype
PrototypePrototype
Prototype
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
javascript
javascript javascript
javascript
 
J query1
J query1J query1
J query1
 
J query
J queryJ query
J query
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
C# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XMLC# Advanced L03-XML+LINQ to XML
C# Advanced L03-XML+LINQ to XML
 
2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance2012 oct-12 - java script inheritance
2012 oct-12 - java script inheritance
 
Designing a JavaFX Mobile application
Designing a JavaFX Mobile applicationDesigning a JavaFX Mobile application
Designing a JavaFX Mobile application
 
70 536
70 53670 536
70 536
 
Javascript Journey
Javascript JourneyJavascript Journey
Javascript Journey
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
jQuery Objects
jQuery ObjectsjQuery Objects
jQuery Objects
 
jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events jQuery -Chapter 2 - Selectors and Events
jQuery -Chapter 2 - Selectors and Events
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
CSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+ReflectionCSharp Advanced L05-Attributes+Reflection
CSharp Advanced L05-Attributes+Reflection
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Java script
Java scriptJava script
Java script
 

Viewers also liked

Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
Luminary Labs
 

Viewers also liked (6)

week2
week2week2
week2
 
Week 1
Week 1Week 1
Week 1
 
Week 1
Week 1Week 1
Week 1
 
Week2
Week2Week2
Week2
 
Week 1 (v3)
Week 1 (v3)Week 1 (v3)
Week 1 (v3)
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar to Week3

AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
SreeVani74
 
JavaScript!
JavaScript!JavaScript!
JavaScript!
RTigger
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Marlon Jamera
 
Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)
Julie Meloni
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
fuser interface-development-using-jquery
fuser interface-development-using-jqueryfuser interface-development-using-jquery
fuser interface-development-using-jqueryKostas Mavridis
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
Pamela Fox
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
Tarek Yehia
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJamshid Hashimi
 
Java script
Java scriptJava script
Java script
Adrian Caetano
 
Cordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptCordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced Javascript
Binu Paul
 
User Interface Development with jQuery
User Interface Development with jQueryUser Interface Development with jQuery
User Interface Development with jQuery
colinbdclark
 
JavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft TrainingJavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft Training
Radoslav Georgiev
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
Sergey Semashko
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans Fabrizio Giudici
 
BITM3730 10-4.pptx
BITM3730 10-4.pptxBITM3730 10-4.pptx
BITM3730 10-4.pptx
MattMarino13
 
BITM3730 10-3.pptx
BITM3730 10-3.pptxBITM3730 10-3.pptx
BITM3730 10-3.pptx
MattMarino13
 

Similar to Week3 (20)

AJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdfAJS UNIT-1 2021-converted.pdf
AJS UNIT-1 2021-converted.pdf
 
JavaScript!
JavaScript!JavaScript!
JavaScript!
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)Learning About JavaScript (…and its little buddy, JQuery!)
Learning About JavaScript (…and its little buddy, JQuery!)
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
fuser interface-development-using-jquery
fuser interface-development-using-jqueryfuser interface-development-using-jquery
fuser interface-development-using-jquery
 
JavaScript Workshop
JavaScript WorkshopJavaScript Workshop
JavaScript Workshop
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
JavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQueryJavaScript Fundamentals & JQuery
JavaScript Fundamentals & JQuery
 
Java script
Java scriptJava script
Java script
 
Cordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced JavascriptCordova training : Day 4 - Advanced Javascript
Cordova training : Day 4 - Advanced Javascript
 
User Interface Development with jQuery
User Interface Development with jQueryUser Interface Development with jQuery
User Interface Development with jQuery
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
JavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft TrainingJavaScript Basics - GameCraft Training
JavaScript Basics - GameCraft Training
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 
Rich Internet Applications con JavaFX e NetBeans
Rich Internet Applications  con JavaFX e NetBeans Rich Internet Applications  con JavaFX e NetBeans
Rich Internet Applications con JavaFX e NetBeans
 
BITM3730 10-4.pptx
BITM3730 10-4.pptxBITM3730 10-4.pptx
BITM3730 10-4.pptx
 
BITM3730 10-3.pptx
BITM3730 10-3.pptxBITM3730 10-3.pptx
BITM3730 10-3.pptx
 

Recently uploaded

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 

Recently uploaded (20)

The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 

Week3

  • 1.
  • 4. Data Types in Javascript • (1) Numbers • (2) Strings • (3) Booleans • (4) Functions • (5) Objects • (6) Undefined
  • 5. FUNCTIONS The ultimate DRY Encapsulate common functionality function name( arg_1, ... arg_n ) { [ FUNCTION BODY ] }
  • 6. CONTROL FLOW if • Execute some code if a certain condition pertains for • Execute some code a “definite” number of times while • Execute some code an “indefinite” number of times
  • 7. CONTROL FLOW: if if ( condition_1) { [ DO SOME STUFF ] } else if ( condition_2 ) { [ DO SOME OTHER STUFF ] } else { [ DO DEFAULT STUFF ] }
  • 8. CONTROL FLOW: for for ( var i = 0; i < 10; i++ ) { [ DO SOME STUFF ] } // That stuff will happen 10 times
  • 9. CONTROL FLOW: while while ( [SOME CONDITION OBTAINS] ) { [ KEEP DOING STUFF ] } // That stuff will keep happening until // the condition is false
  • 10. Where do we code our javascript?
  • 14. 4. Include a .js file
  • 15. 5. In Node.js Terminal
  • 17. FUNCTIONS AS ARGUMENTS In Javascript, functions can be passed as arguments to other functions • Relatedly, they can be returned by functions and stored as variables • This is a very special and valuable feature of the language • One says that functions are “first class objects” • Javascript, in this way, treats functions like data var say_hi = function() {console.log(“hi”);}; say_hi(); // Prints “hi”
  • 18. BUZZZZWORD “First class objects” • These are the “objects” in an “object-oriented” language (we will define “object oriented” later) that can be (1) saved in variables, (2) passed as arguments to functions, and (3) returned as values from functions
  • 19. ANONYMOUS FUNCTIONS Javascript conveniently allows you to define functions “inline” without specifying a name var kill_timeout = set_timeout( function() { console.log(“You are now 5 seconds older”); }, 5000); // Javascript tells time in milliseconds
  • 20. RECURSIVE FUNCTIONS In Javascript, functions can call themselves • This is called “recursion” • It must “bottom out” at the “base case” or it will never end (and you will run out of memory!) function factorial(n) { if ( n == 0 ) { return 1; // Factorial of zero is one by definition } else { return n * factorial( n - 1 ); // Recursive step } }
  • 22. ARRAYS Javascript [data structures]
  • 23. ARRAYS A data structure native to Javascript that stores values in an ordered way • Arrays contain an arbitrary number of “elements” which can be accessed individually by “indexing” into the array • Defined by comma separating values between brackets • Arrays are “indexed” from zero (not from one) var team = [‘ben’,‘jeff’,‘kam’]; team[0] // => ‘ben’ team[2] // => ‘kam’ team[3] // => undefined
  • 24. OBJECTS Javascript [data structures]
  • 25. BUZZZZWORD “Object Oriented” • A way of structuring programming languages invented in the early 90s • Data and functions (“methods”) are grouped together in “objects” • The outside world can interact with these objects only through a rigidly defined interface • Javascript is centered on objects, but not object oriented in the traditional sense
  • 26. OBJECTS Objects are collections of “properties” • Properties are key / value pairs • Defined using braces • Use “dot” notation to read and write var person = {}; person.name = ‘will’; // Write var name = person.name; // Read
  • 27. OBJECTS Can also use “javascript object notation” to get and set properties var person = {}; person[ ‘name’ ] = ‘will’; // Write var name = person[ ‘name’ ]; // Read
  • 28. OBJECTS Everything in Javascript is an object • Therefore everything can have properties • Some values have “built in” properties var name = ‘will’; name.length; // => 4 var team = [‘ben’, ‘jeff’, ‘kam’]; team.length; // => 3
  • 29. OBJECTS Functions stored as properties are called “methods” var team = [‘ben’, ‘jeff’]; team.push( ‘kam’ ); // => team[2] == ‘kam’ // Push is an Array method that adds a value to the end of an array
  • 30. “this” “this” is used to refer to an object inside the body of a method var person = {}; person.name = ‘will’; person.say_name = function() { console.log( this.name ); } person.say_name(); // => ‘will’
  • 31. THEORY VS. PRACTICE There is a fair amount of theory underlying object orientation For our practical purposes, at the moment you need only understand what properties are, and how to set (write) and get (read) them var obj = {}; obj.prop = ‘some_val’; // Setting var val = obj.prop; // Getting
  • 32. Document Object Model (DOM) [ how javascript interacts with page content ] Introduction
  • 33. Document Object Model • HTML documents have hierarchy • every element has a parent • every parent thus has children
  • 34. Document Object Model The tree is made up of nodes and text
  • 35. DOM Traversal: Node Links x.childNodes x.parentNode x.firstChild x.lastChild x.nextSibling x.previousSibling not enough for ya? https://github.com/spicyj/jquery-genealogy
  • 36. Problem: Not Sustainable document.firstChild.lastChild.previousSibling.firstChild.nextSibling.style.display = “none” VS. document.getElementById(“dummy”).style.display = “none”;
  • 37. BUZZZZWORD SUSTAINABLE • Sustainable code retains its integrity and function over time. • It is invulnerable to external influence!
  • 38. DOM Traversal: Attribute Selection var my_box = document.getElementById(“my-unique-id”); var my_links = document.getElementsByTagName(“A”);
  • 39. Important Properties x.innerHTML document.innerHTML(“<p> oh no! </p>”); x.style document.body.style.marginTop(“30px”); x.getAttribute(name) my_link.getAttribute(“href”); // => “http://www.hackyale.com” x.setAttribute(name, value) my_link.setAttribute(“href”, “http://www.20b.org/”);
  • 40. The ‘document’ Object The document is a special object, with it’s methods and properties.
  • 41. PUTTING IT ALL TOGETHER Let’s put cats on stuff

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n