IFN666Lecture2 2
IFN666Lecture2 2
Faculty of Science
https://www.interviewbit.com/javascript-interview-questions/#is-
javascript-a-statically-typed-or-a-dynamically-typed-language
• Functions
– Normal function
– Anonymous functions
– Arrow functions
– Higher order functions
• More on Objects, Arrays, and Functions
• JSON
• Asynchronous JavaScript
• Modules
• JavaScript Engine
FUNCTIONS
let carName;
carName = "Volvo";
carName = "Volvo";
let carName;
To avoid bugs, always declare all variables at the 7
a university for the real world
R
console.log(quartic(3));
console.log(cube(4));
FUNCTIONAL JAVASCRIPT
45
var elements = [
'Hydrogen',
'Helium',
'Lithium',
'Beryllium'
];
elements.map((e) => {
return e.length;
}); // [8, 6, 7, 9]
let points = [40, 100, 1, 5, 25, 10];
points.sort(); // wrong sorting result
points.sort(function(a, b){return a - b});
function myFunction(value) {
txt = txt + value +"\t" ;
console.log(txt);
}
I
for (let i = 0; i < journal.length; i++) { I love
myFunction (journal[i]); // Do something with entry I love you
}
I
txt = ""; I love
journal.forEach(myFunction); // Do something with entry I love you
// display Array I
let text = ""; love
let journal = ["I", "love", "you"]; you
for (let x of journal) {
text += x + "\n";
}
J
a
// display String
v
let text = "";
a
let language = "JavaScript";
S
for (let x of language) {
c
text += x + "\n";
r
}
i
p
a university for the real world
R
22
t
CRICOS No. 00213J
MORE ON ARRAY, FUNCTION,
AND OBJECT
a university for the real world
R
23
CRICOS No. 00213J
Functions
Defining a function to create an object
real world
R
a university for the
24
CRICOS No. 00213J
Objects
real world
R
a university for the
25
CRICOS No. 00213J
Objects
real world
R
a university for the
26
CRICOS No. 00213J
Functions
real world
R
a university for the
27
CRICOS No. 00213J
Functions
function house(rms, stl, yr, garp) {
this.length = 5;
this.rooms = rms;
this.style = stl;
this.yearBuilt = yr;
this.hasGarage = gar;
this.show = mshowHouse;
}
real world
R
a university for the
28
CRICOS No. 00213J
Functions
function mshowHouse( ) {
var nprops = this.length;
for (let iter = 1; iter < nprops; iter++) {
console.log(“Property ” + iter + “ is ” +
this[iter]);
}
}
myhouse.show( );
real world
R
a university for the
29
CRICOS No. 00213J
Date, RegExp, JSON
MORE ON JS OBJECTS
real world
R
a university for the
31
CRICOS No. 00213J
JavaScript built-in objects
http://www.w3schools.com/js/
String Array
• length • length
• charAt() • concat()
• concat() • sort()
• indexOf()
• reverse()
• replace()
• shift()
• substr()
• pop()
• split()
• push()
str = "this is a string";
i = str.indexOf('a'); let a = [banana, apple,
pear];
a = "[email protected]".split('@'); a.sort();
real world
R
a university for the
32
CRICOS No. 00213J
JavaScript built-in objects - Date
http://www.w3schools.com/js/
• Date objects are static.
• Date objects are created with the new Date() constructor
• There are 4 ways to create a new date object:
1. new Date()
2. new Date(year, month, day, hours, minutes, seconds, milliseconds)
3. new Date(milliseconds)
4. new Date(date string)
let d = new Date(); // creates a new date object with the current date and time
console.log(d); // output ISO date 2021-02-28T03:56:16.268Z
real world
R
a university for the
33
CRICOS No. 00213J
JavaScript built-in objects - Date
http://www.w3schools.com/js/
let d = new Date(2021, 2, 24, 10, 33, 30, 0);
// 7 numbers specify year, month, day, hour, minute, second, and millisecond
d = new Date(2021, 2, 24, 10, 33, 30);
// 6 numbers specify year, month, day, hour, minute, second
d = new Date(2021, 2, 24, 10, 33);
//5 numbers specify year, month, day, hour, and minute
d = new Date(2021, 2, 24, 10);
// 4 numbers specify year, month, day, and hour
d = new Date(2021, 2, 24);
// 3 numbers specify year, month, and day
d = new Date(2021, 2); // 3 numbers specify year, month
real world
R
a university for the
34
CRICOS No. 00213J
JSON
let you = {
"firstName": "John",
. JSON.stringify()
"lastName": "Smith",
"isAlive": true, obj = JSON.stringify(you);
"age": 25,
"height_cm": 167.64,
"phone": [
{ "type": "home",
"number": "07 1234 5678" },
{"firstName":"John","lastName":
{ "type": "mobile", "Smith","isAlive":true,"age":25,"
"number": "0408 123 456" } height_cm":167.64,"phone":
],
"toString": function() {
[{"type":"home","number":"07
return this.firstName + this.lastName; 1234 5678"},
}
{"type":"mobile","number":"040
};
8 123 456"}]}
console.log(you.phone[0].number); //07 1234 5678
real world
R
a university for the
38
CRICOS No. 00213J
JSON Data Types
myObj = { "name":"John", "age":30, "car":null };
for (key in myObj) {
console.log(key, myObj[key]);
ASYNCHRONOUS JAVASCRIPT
PROMISES, PROMISES
img1.ready()
.then(function() {
// loaded, so do stuff
})
.catch(function(error) {
// report on an error
});
Here we are assuming that the image has a ready method that returns a
promise. It doesn’t.
This is the skeleton for the creation of a Promise. Initially we will just consume
them, as we do in the fetch()examples that will appear soon.
foo.js
const circle = require('./circle.js');
square.js
module.exports = (side) => {
return {
area: () => side * side
};
};
Here we see square exposed as the module type. Use this approach
if you wish to have a particular function as the root export.
main.js
//------ main.js ------
import { square, diag } from './lib';
const str = "Square: " + square(11) + "<br>" + "Diag: " + diag(4,3);
document.getElementById("app").innerHTML = str;
Square: 121
Diag: 5
JAVASCRIPT ENGINE
real world
R
a university for the
64
CRICOS No. 00213J
Client Scripts
real world
R
a university for the
65
CRICOS No. 00213J
Server Scripts
real world
R
a university for the
66
CRICOS No. 00213J
CONCLUSION
• Functions
– Normal functions
– Anonymous and arrow functions
– Chaining in functions
• More on Objects, Arrays, and Functions
• JSON
• Asynchronous JavaScript
• Modules
• We will learn event handling and error handling in later
weeks.
1. Objects
2. Arrays
3. Functions
https://www.codingame.com/work/javascript-interview-questions/
https://www.interviewbit.com/javascript-interview-questions/#javscript-data-types
• {
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
real world
R
a university for the
CAB230 - Lecture 5 78
CRICOS No. 00213J
AMS Exercises
https://sefams01.qut.edu.au/IFN666/
• 4 Questions
• 10 Marks
– 2,2,3,3
• 15 chances per question
• average: Write a basic function to calculate and return
the average of two numbers.