PROGRAMOWANIE JAVASCRIPT

Programowanie JavaScript 03

Javascript

Array

let array = []
let array = Array()
let array = new Array()
let array = ["a", 1, {}]
let array = Array("a", 1, {})
let array = new Array("a", 1, {})
let array = new Array(10)

Map object

let countryCodes = new Map()
countryCodes.set('Poland', 'pl')
countryCodes.set('Germany', 'de')
countryCodes.size
countryCodes.get('Russia')
countryCodes.has('Spain')
countryCodes.delete('Germany')
countryCodes.has('Germany')
countryCodes.set('Poland', 'hmm')
countryCodes.clear()
countryCodes.size

WeakMap object

Prawie jak Map, ale tylko dla typów Object.

Set

let countrySet = new Set()
countrySet.add('Poland')
countrySet.add('Germany')
countrySet.size
countrySet.has('Russia')
countrySet.delete('Poland')
countrySet.add('Poland')
countrySet.add('Poland')
countrySet.size
Array.from(countrySet)

Object

let myCar = new Object();
myCar.make = 'Ford';
myCar.model = 'Mustang';
myCar.year = 1969;
myCar.property1 = "value1"
myCar.property2 = 3.14
myCar.3 = 14
console.log(myCar)
myCar['property2']
myCar['property3'] = '19,999 USD'
console.log(myCar['property4'])
let property3Variable = 3
myCar[property3Variable] = 14
console.log(myCar[property3Variable])
let myBike = {3: 14, 'make': 'Ducati', 'year': 2018}
function Car(make, model, year) {
  this.make = make
  this.model = model
  this.year = year
}

myCar = new Car('Fiat', '126p', 1993);
myCar.make
myCar.model
myCar.year
myCar.year = 1994
myCar.color = "red"

owner = {'firstName': 'Joe', 'lastName': 'Doe'}

myCar.owner = owner
myCar.owner.firstName
function Car(make, model, year) {
  this.make = make
  this.model = model
  this.year = year
}

Car.prototype.color = 'black'

myCar = new Car('Fiat', '126p', 1993);
myCar.make
myCar.model
myCar.year
myCar.year = 1994
myCar.color
function Car(make, model, year) {
  this.make = make
  this.model = model
  this.year = year

  this.age = function() {
    return new Date().getFullYear() - this.year
  }

  this.update = function(year, model) {
    this.model = model
    this.year = year
  }
}

myCar = new Car('Fiat', '126p', 1993);
myCar.age()
myCar.update(2000, 'Tesla')
console.log(myCar)

Ćwiczenie

  1. ile razy musze wylosować liczbę z zakresu [0, 1] tak, aby suma liczb była większa od 1 (Math.random). Wynik, każdej próby zapisz do tablicy, policz średnią dla 1_000 prob.
  2. Na podstawie Set(2) {"de", "pl"} i Map(2) {"Poland" => "pl", "Germany" => "de"} wygeneruj mape, Map(2) {"Poland" => 1, "Germany" => 0} gdzie 1, 0 to pozycje kodu kraju w secie.