Class Javascript tutorial 2
Getter a Setter
Získej nebo Nastav.
Michal už není Váš kamarád, tak jej změníte pomocí set:
Získej nebo Nastav.
Třídy v javascript dovolují použití get a set instrukci.
get vrátí hodnotu určité zadané proměnné.
set jí může změnit.
class Person {
constructor(name) {
this.personName = name;
}
get pname() {
return this.personName;
}
// i kdyz set vypada jako funkce, musi mit pouze jeden parametr
set pname(x) {
this.personName = x;
}
}
var myFriend = new Person("Michael");
Jméno osoby přítele získáme například:
var strMyFriendName = myFriend.pname; // bez () na konci!!!!!!
alert(strMyFriendName);
Michal už není Váš kamarád, tak jej změníte pomocí set:
// hodnota se nepřirazuje do závorek (), ale pomocí = rovná se!!!!
myFriend.pname = "Renata";
strMyFriendName = myFriend.pname;
alert(strMyFriendName);
77LW NO topic_id
AD
Další témata ....(Topics)
// IF variable is not defined (or has a falsey value) THEN set it to an empty object.
// ELSE do nothing (technically speaking, variable gets assigned to itself)
var variable = variable || {} ;
function test(){
console.log(1);
}
// when called
test(); // 1
// or
var a = test;
a(); // 1
// or autocall if code executed
var test2 = function(){
console.log(2);
}
// test2 is undefined because the function test2 has no return value
// return string
function stringTest(param) {
return 'Hello ' + ' ' + param;
}
console.log(stringTest("Alice")); // Hello Alice
// return number
function numberTest(param) {
return 6 + param;
}
console.log(numberTest(5)); // 11
// or autocall if code executed
// function is like other variables
// parenthesis is for made groups or call functions.
// Immediately-Invoked Function Expression,
// or IIFE for short. It executes immediately after it’s created.
(function(){
// all your code here
var foo = function() {};
window.onload = foo;
// ...
})();
// foo is unreachable here (it’s undefined)
//////////////////////////////
// IIFE Immediately Invoked Funtion Expression
var greeting = function(name) {
return 'Hello ' + ' ' + name;
}('John');
console.log(greeting); // Hello John
console.log(typeof greeting); // string
console.log(greeting('Suzan')); // Uncaught TypeError: greeting is not a function
///////////////////////
var val = (function(){
var a = 0; // in the scope of this function
return function(x){
a += x;
return a;
};
})();
alert(val(10)); //10
alert(val(11)); //21
///////////////////////
// create the structure
var testHash = {
a : 1,
b : function(){
console.log(4);
},
c: function(param){
var num = param +5;
return num;
}
}
console.log(testHash.a); // 1
testHash.b(); // 4
testHash['b'](); // 4
testHash.c(7); // 12
/////////////////////////
// or function like class
function TestClass(n) {
this.some_property = n;
this.some_method = function() {
console.log(this.some_property);
};
}
var foo = new TestClass(3);
var bar = new TestClass(4);
foo.some_method(); // 3
bar.some_property += 2;
bar.some_method(); // 6
// console.log(some_property); // Uncaught ReferenceError: some_property is not defined
//////////////////////////
function Car(brand, year) {
return {
brand: brand,
year: year
}
}
var honda = Car("Honda", 1999);
console.log(honda.brand); // Honda
console.log(honda.year); // 1999
honda.brand = "Honda new Generation";
honda.year = 2021;
console.log(honda.brand); // Honda new Generation
console.log(honda.year); // 2021
var mazda = Car("Mazda",1987);
console.log(honda.brand); // Honda new Generation
console.log(mazda.brand); // Mazda
//////////////////////////////////////
// simplified code
var TEST_CLASS = TEST_CLASS || {};
TEST_CLASS.pokus = TEST_CLASS.pokus || (function() {
// -------------------------------------------------------------------------
// Private static variables and methods
// -------------------------------------------------------------------------
var privateStaticNumber = 5;
var privateStaticString = "My privateStaticString: ";
var privateStaticArray = [];
function privateStaticMethod(){
console.log('privateStaticMethod');
}
// CONSTRUCTOR CTOR
return function(config) {
// ----- private variables -----
var me = this,
isPaused = false;
this.privateStaticNumber = config.firstParam;
this.privateStaticString = privateStaticString + config.secondParam; // += produce: undefined I am from secondParam
this.privateStaticArray = config.thirtParam;
// ----- private methods -----
function getMode (mode, speed) {
// document.getElementById(mode).addEventListener('click', function () { snakeSpeed = speed; });
}
// ----- public variables -----
me.correct = 0;
me.wrong = 0;
// ----- public methods -----
me.setString = function(val) {
this.privateStaticString = val;
};
me.getString = function() {
return this.privateStaticString;
};
me.publicMethod = function(param){
console.log(param + ' from me.publicMethod()');
};
}; // end CTOR
})();
// end TEST_CLASS.pokus
var _me = _me || {};
var _me_test = new TEST_CLASS.pokus({firstParam:8, secondParam:" I am from secondParam", thirtParam:[1,3,9]});
// TEST_CLASS.pokus.privateStaticMethod(); // is not a function
// TEST_CLASS.pokus.publicMethod("Hola!"); // TEST_CLASS.pokus.publicMethod is not a function
// TEST_CLASS.publicMethod("Hola hej!"); // TEST_CLASS.publicMethod is not a function
// TEST_CLASS.pokus.publicMethod(" Hello!"); // is not a function
_me_test.publicMethod(" Hello!"); // Hello! from me.publicMethod()
console.log( _me_test.getString()); // My privateStaticString: I am from secondParam
console.log( _me_test.isPaused); // undefined
Date: 04.11.2020 - 23:14Ukázky zaokrouhlení čísla v JavaScript.
var num = 123.45678;
var rounded = Math.round(num); // out: 123
var num = 123.5269;
var rounded = Math.round(num); // out: 124
var num = -3.5269;
var rounded = Math.round(num); // out: -4
var num = 3.5269;
var rounded = Math.round(num * 100)/100; // out: 3.52
var num = 3.5269;
var rounded = Math.round(num * 1000)/1000; // out: 3.526
var num = 3.5269;
var rounded = num.toFixed(3); // out: 3.527
c:xampphtdocs
var 1_radian_to_degree = 180 / Math.PI; // 57,295779513082320876798154814105 degree
var 1_degree_to_radian = Math.PI / 180; // 0,01745329251994329576923690768489 radian
var 360_degree = 2 * Math.PI * 1_radian_to_degree;
var degree = 36.86;
var opposite = 600;
var hypotenuse = 1000;
var adjacent = 800;
// degree
// sin 45° == 0.7071067811865475;
var sin = Math.sin(45 * Math.PI / 180.0)
//sin == 0.7071067811865475;
// vypocet prepony (hypotenuse), kdyz zname
// protilehlou (opposite) a uhel (degree)
hypotenuse2 = opposite/ Math.sin(degree * Math.PI / 180.0);
// vypocet protilehle (opposite)
opposite2 = hypotenuse * Math.sin(degree * Math.PI / 180);
// vypocitat uhel alfa z velikosti protilehle a prepony ve stupnich (degree)
degree2 = Math.asin(opposite / hypotenuse)* (180/Math.PI); // 1rad
// velikost prepony pomoci prilehle (adjacent) strany a cos a uhlu v stupnich (degree)
hypotenuse3 = adjacent / Math.cos(degree * Math.PI / 180);
// velikost prilehle (adjacent) strany pomoci prepony a cos a uhlu v stupnich (degree)
adjacent2 = hypotenuse * Math.cos(degree * Math.PI / 180);
tan = Math.tan(degree * Math.PI/180); // 45° == 0.9999999999999
degree3 = Math.atan(opposite / adjacent)* (180/Math.PI); // 1rad
adjacent3 = opposite / Math.tan(degree * Math.PI / 180);
opposite3 = adjacent * Math.tan(degree * Math.PI / 180);
Textarea selection - získání pozice karety kurzoru.
// FOR Gecko Browser selectionStart / selectionEnd selection length
function getPos(){
if (typeof document.getElementById('idTextarea').selectionStart != 'undefined') {
var nPosCursor = document.getElementById('idTextarea').selectionStart;
var nEndPos = document.getElementById('idTextarea').selectionEnd;
return nPosCursor;
}
return 0; // or -1
}
// source by javascript-array.com/guides/javascript_faq/
//Internet Explorer 4+, Mozilla/Gecko/Firefox
function getCaretPos(obj)
{
obj.focus();
if(obj.selectionStart) return obj.selectionStart; //Gecko
else if (document.selection) //Internet Explorer
{
var sel = document.selection.createRange();
var clone = sel.duplicate();
sel.collapse(true);
clone.moveToElementText(obj);
clone.setEndPoint('EndToEnd', sel);
return clone.text.length;
}
return 0;
}
Udaje o prohlížeči
alert(navigator.appName); // Netscape
alert(navigator.appVersion); // 5.0(Windows; en-US)
alert(navigator.appCodeName); // Mozilla
alert(navigator.appMinorVersion); // subverze prohlížeče
alert(navigator.browserLanguage);
alert(navigator.cookieEnabled); // true - false
alert(navigator.cpuClass);
alert(navigator.onLine); // true když je připojen k síti
alert(navigator.platform);
alert(navigator.systemLanguage);
alert(navigator.userAgent);
alert(navigator.userLanguage);
// zjisteni detekce prohlizece Google Chrome
var bool_is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
Editace: 18.5.2020 - 22:44
Počet článků v kategorii: 77
Url:class-javascript-tutorial-2