?print-pdf
' Създаден от
// using built in Math object
console.log( Math.max(2,12,5)); // 12
// using built in Date() constructor
let currentDate = new Date();
console.log( currentDate.getFullYear() ); // 2018
Някои от най-често използваните вградени обекти/функции са:
Ако сте любопитни относно другите: Standard built-in objects @MDN
Date()
let currentDateTimeObj = new Date();
See the Pen getCurentTimeFormated by Iva Popova (@webdesigncourse) on CodePen.
See the Pen calcUserAge by Iva Popova (@webdesigncourse) on CodePen.
See the Pen Date object for timing code by Iva Popova (@webdesigncourse) on CodePen.
// using built in arguments object - available within functions only!
function sum(){
sum = 0;
for (let i = 0; i < arguments.length; i++){
sum += arguments[i];
}
console.log("sum = " + sum);
}
sum(1,2,3); // 6
sum(1,2,3,4); // 10
// Създаваме обект от тип String
let str1 = new String("ada");
// Създаваме стринг, който не е обект
let str2 = "ada";
console.log( typeof(str1) ); // object
console.log( typeof(str2) ); // string
console.log(str1 == str2); // true (сравнение по стойност)
console.log(str1 === str2); // false (различни типове)
// примитивните типове не са обекти и нямат пропъртита, но следното работи:
"ada".toUpperCase(); // "ADA"
// стойността "ada" временно се обвива в обект String, върху който се извиква метода
(new String("ada")).toUpperCase()
let str1 = new String("ada");
let str2 = "ada";
str1.id = 1;
str2.id = 1;
console.log(str1.id);// 1
console.log(str2.id);// undefined
// създаваме число като примитив (добър вариант)
let x = 42;
// създаваме число като обект (ненужно сложно)
let y = new Number(42);
// използваме ги по един и същ начин:
console.log( x + 10 ); // 52
console.log( y + 10 ); // 52
console.log( x.toPrecision(5) ); // "42.000"
console.log( y.toPrecision(5) ); // "42.000"
// но не бива да забравяме, че "x" и "y" са различни типове
console.log( x == y ) // true
console.log( x === y ) // false
These slides are based on
customised version of
framework