0 Members and 1 Guest are viewing this topic.
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <script src="main.js" defer></script> </head> <body></body></html>
"use strict";const firstName = "Marija";const firstFun = () => { let x = 10; const y = secondFun(5); x = x + y; return x;};function secondFun(a) { let z = 20; return z + a;}const funCall = firstFun();//execution context: variable environment (promenljive, deklaracija funkcija i objekat argumenata), scope chain, this keywordconst num = 20;function saberi(a) { return a + num;}const result = saberi(10);console.log(result);// dve faze: faza kreiranja i faza izvrsavanja//bez obzira na velicinu projekta imamo jedan globalni execution context//scope -okruzenje u kojem je odredjena promenljiva "vidljiva".//3 vrste scope-a: globalni, function scope, block scope.function outerFunction() { let h = 5; function inner() { let k = 20; console.log(h); } inner(); // console.log(k);}outerFunction();// console.log(k);
//3 vrste scopa: GLOBALNI, FUNKCIJSKI I BLOCK scope//scope - ceo "region" gde se toj promenljivoj moze pristupiti//GLOBALNI SCOPE// promenljive deklarisane izvan funkcije ili bloka//FUNKCIJSKI SCOPE -LOKALNI SCOPE//svaka funkcija kreira svoj scopefunction scopeFun(x) { let y = 12; console.log(x);}scopeFun(5);// console.log(y); // nije vidljiv izvan ove funkcije/viticaste zagrade// console.log(x); // nije vidljiv izvan ove funkcije/viticaste zagrade//BLOCK SCOPE // - novi od skoro kada su uvedeni u js let i const// if(uslov){// }// for( let i=0;i<=uslov;i++){// }//block scope se primenjuje samo na promenljive deklarisane sa let i const//promenljive deklarisane sa VAR -ignorisu block scope// VAR vise ne koristimo jer moze da da drugaciji rezultat u odnosu na ono sto ocekujemo// use strict osigurava da uvek koristimo block scopeif (true) { var x = 15;}// console.log(x);console.log(this);//scope chain-function outter() { const a = 20; function inner() { const b = 15; console.log(b); console.log(a); } inner();}outter();
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <link rel="stylesheet" href="style.css" /> <script src="main.js" defer></script> </head> <body> <p class="message"></p> <img alt="koala" /> <button>Click</button> </body></html>
.fadeIn { opacity: 0; animation: fadein 2s ease-in forwards;}@keyframes fadein { to { opacity: 1; }}
"use strict";//sinhroni kod- kod se izvrsava liniju po linijuconst pElement = document.getElementsByClassName("message")[0];// pElement.textContent = "My name is Marija";// alert("Text is set!"); - blokirajuci kod// pElement.style.color = "red";//asinhroni kod sa tajmeromsetTimeout(function () { //-neblokirajuci kod pElement.textContent = "My name is Marija";}, 10000);pElement.style.color = "red";console.log("Poslednja linija koda");const btn = document.getElementsByTagName("button")[0];btn.style.backgroundColor = "blue";//II primerconst imgElement = document.getElementsByTagName("img")[0];console.log(imgElement);imgElement.src = "koala.jpg";imgElement.addEventListener("load", function () { //load event je asihroni deo koda imgElement.classList.add("fadeIn");});imgElement.style.width = "300px";//AJAX- asinhroni js i XML
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <script src="main.js" defer></script> </head> <body> <p></p> </body></html>
JSONconst jsonShema = { name: "Product", properties: { id: { type: "number", description: "Product identifier", required: true, }, name: { type: "string", description: "Name of product", required: true, }, price: { type: "number", minimum: 0, required: true, }, tags: { type: "array", items: { type: "string", }, }, stock: { type: "object", properties: { warehouse: { type: "number", }, retail: { type: "number", }, }, }, },};
//AJV- another JSON validator// ajv terminal // terminal>npm install ajv //// KAD SMO U TERMINALU DESNO U ISTOJ LINIJI GLEDAMO DO + PA ONDA ZNAK KAO STRELICA NA DOLE ∨// I TU OTVORIMO COMMAND PROMPT (cmd) //>npm install ajv
//JSON// const jsonShema = {// name: "Product",// properties: {// id: {// type: "number",// description: "Product identifier",// required: true,// },// name: {// type: "string",// description: "Name of product",// required: true,// },// price: {// type: "number",// minimum: 0,// required: true,// },// tags: {// type: "array",// items: {// type: "string",// },// },// stock: {// type: "object",// properties: {// warehouse: {// type: "number",// },// retail: {// type: "number",// },// },// },// },// };//AJV- another JSON validator// ajv terminal // terminal>npm install ajv //// KAD SMO U TERMINALU DESNO U ISTOJ LINIJI GLEDAMO DO + PA ONDA ZNAK KAO STRELICA NA DOLE ∨// I TU OTVORIMO COMMAND PROMPT (cmd) //>npm install ajvconst schema = { type: "object", properties: { id: { type: "number" }, name: { type: "string" }, price: { type: "number", minimum: 200, }, productDescription: { type: "array", items: { type: "string" }, }, stock: { type: "object", properties: { wholesale: { type: "number" }, retail: { type: "number" }, }, }, }, required: ["id", "name", "price"],};const data = { id: 126, name: "Car256", price: 20000, productDescription: ["car", "Audi", "A6"], stock: { wholesale: 44000, },};// const Ajv = require("ajv");// const ajv = new Ajv();// const validate = ajv.compile(schema);// const valid = validate(data);// if (valid) {// console.log("✔ Podaci su validni!");// } else {// console.log("❌ Podaci nisu validni!");// console.log(validate.errors);// }//konvertovanje u JSON - JSON.stringifyconst jsonData = JSON.stringify(data);console.log(jsonData);const paragraphEl = document.getElementsByTagName("p")[0];paragraphEl.textContent = jsonData;console.log(typeof jsonData);//konvertovanje u JS objekat- JSON.parseconst jsObj = JSON.parse(jsonData);console.log(jsObj);//js niz u JSONconst imena = ["Maja", "Sara", "Dunja"];const jsonNiz = JSON.stringify(imena);paragraphEl.textContent = jsonNiz;//JSON u js nizconst niz = JSON.parse(jsonNiz);console.log(niz);//Date kao objconst text = ' { "name":"John", "birth" : "1986-12-14" , "city" : "New York " } ';const obj = JSON.parse(text);console.log(obj);obj.birth = new Date(obj.birth);console.log(typeof obj.birth);//Funkcija kao objconst jsonInput = ' { "name" : "John" , "age":"function() { return 30 ; }" , "city" : "New York" } ';const newObj = JSON.parse(jsonInput);console.log(newObj);console.log(typeof newObj.age);newObj.age = eval("(" + obj.age + ")");// console.log(newObj.age());console.log(newObj);//new Date konvertovanje u jsonconst john = { name: "John", today: new Date(), city: "New York" };const jsonJonh = JSON.stringify(john);console.log(jsonJonh);const johnObj = JSON.parse(jsonJonh);console.log(johnObj);//funkcija u jsonconst mike = { name: "Mike", age: function () { return 30; }, city: "New York",};const jsonMike = JSON.stringify(mike);console.log(jsonMike); //{"name":"Mike","city":"New York"} - metoda ce izbrisati funkciju i kljuc i vrednost//pretvaranje funkcije u stringmike.age = mike.age.toString();console.log(mike.age);
// DA BI OVO ISPOD RADILO "requre" deo, idemo u TERMINAL i kucamo// da dodjemo do JSON foldera gde su indexhtml i mainjs// cd JSON// znaci >cd JSON// i u sledecoj liniji u terminalu kucamo ime fajla// >node main.jsconst Ajv = require("ajv");const ajv = new Ajv();
const schema = { type: "object", properties: { id: { type: "number" }, name: { type: "string" }, price: { type: "number", minimum: 200, }, productDescription: { type: "array", items: { type: "string" }, }, stock: { type: "object", properties: { wholesale: { type: "number" }, retail: { type: "number" }, }, }, }, required: ["id", "name", "price"],};const data = { id: 126, name: "Car256", price: 20000, productDescription: ["car", "Audi", "A6"], stock: { wholesale: 44000, },};// DA BI OVO ISPOD RADILO "requre" deo, idemo u TERMINAL i kucamo// da dodjemo do JSON foldera gde su indexhtml i mainjs// cd JSON// znaci >cd JSON// i u sledecoj liniji u terminalu kucamo ime fajla// >node main.jsconst Ajv = require("ajv");const ajv = new Ajv();const validate = ajv.compile(schema);const valid = validate(data);if (valid) { console.log("✔ Podaci su validni!");} else { console.log("❌ Podaci nisu validni!"); console.log(validate.errors);}
<?xml version=1.0 encoding="UTF-8"?><korisnik> <ime>Marko</ime> <prezime>Markovic</prezime> <email>m.markovic@gmail.com</email> <adresa> <ulica>Kralja Petra 12</ulica> <grad>Beograd</grad> </adresa></korisnik>
JSON sintaksa• JSON: jsonObj = { "name":"John", "age":31, "city":"New York" };• JavaScript: jsObj = { name:"John", age:31, city:"New York" };
// const promise = new Promise((resolve, reject) => {// const x = 10;// const y = 3;// if (x === y) {// resolve({ status: "uspesno realizovan promise", statusCode: 200 });// } else {// reject({// reason: "Page not found",// statusCode: 404,// });// }// });// console.log(promise);//AJAX pozivfunction getData() { const data = fetch("https://jsonplaceholder.typicode.com/todos/1") .then((resolve) => { console.log(resolve); return resolve.json(); }) .then((result) => console.log(result)); console.log(data);}getData();//zivotni ciklus promisa:// 1.pending -pocetno stanje, pre nego sto se ijedna vrednost vrati//2.settled: fulfilled, rejected