javascript js functions like old class example not ECMAScript 2015 ES6
// 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:1477LW NO topic_id
AD
Další témata ....(Topics)
HTTP to HTTPS
# only one RewriteEngine On can be used in htaccess!!!!!!!!!!!!!!
RewriteEngine On
# all redirection HTTP -> HTTPS
# HTTPS off / if start with http.....
# off equality !=on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
example.com to www.example.com
# redirection no www -> https://www.
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Change sitemap.txt to sitemap.php if url //domain.com/sitemap.txt
# //domain.com/sitemap.txt open sitemap.php
# in sitemap.php is function what return list of address
RewriteRule ^sitemap\.txt$ sitemap.php [L]
RewriteRule ^sitemap$ sitemap.php [L]
Remove www. before subdomain name
www.subdomain.example.comto
subdomain.example.com
RewriteCond %{HTTP_HOST} (^|\.)(www\.)([^\.]*)\.example\.com$ [NC]
RewriteRule (.*) https://%3.example.com/$1 [R=301,QSA,L]
Remove dust before subdomain.example.com
# www.bla.www.m.bla.subdomain.example.com to subdomain.example.com
RewriteCond %{HTTP_HOST} (.*)(subdomain.example.com$) [NC]
RewriteRule (.*) https://subdomain.example.com/$1 [R=301,QSA,L]
Remove only one www. before domain name
# redirection www. -> https://
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Testování regulárních výrazů - regex
https://regex101.com/
Je možno zadat příchozí adresu a nechat jí upravit, pak znovu nechat projít
Zadá se adresa //bla.bl?query_string=testovany_string
Do velkého pole se zadají
RewriteCond
RewriteRule
a klikne se na TEST
https://htaccess.madewithlove.be/
Přesměrování stránek
https://www.sslmentor.cz/napoveda/presmerovani-https-pomoci-htaccess
Easily check status codes, response headers, and redirect chains.
Kontrola jak probíhá přesměrování s jednotlivými hlášeními - 301, 200, 404 atd.
https://httpstatus.io/
https://regex101.com/
Je možno zadat příchozí adresu a nechat jí upravit, pak znovu nechat projít
Zadá se adresa //bla.bl?query_string=testovany_string
Do velkého pole se zadají
RewriteCond
RewriteRule
a klikne se na TEST
https://htaccess.madewithlove.be/
Přesměrování stránek
https://www.sslmentor.cz/napoveda/presmerovani-https-pomoci-htaccess
Easily check status codes, response headers, and redirect chains.
Kontrola jak probíhá přesměrování s jednotlivými hlášeními - 301, 200, 404 atd.
https://httpstatus.io/
Jak naformátovat číslo například 1000 aby výstup byl 1 000.
How formatting Numbers in javascript
// in us return 1,000.00 in czech cz return 1 000,00
var newNumber = format_Of_Number ('cz', 1000.00); // cz is czech format - us is us format of number
function format_Of_Number (stat, num) {
stat = stat.toLowerCase();
nStr = this + '';
var x = nStr.split('.');
var x1 = x[0];
var x2;
if(stat=='cz')
x2 = x.length > 1 ? ',' + x[1] : ''; // nahradime tecku carkou
else if(stat=='us')
x2 = x.length > 1 ? '.' + x[1] : ''; // us format
var rgx = /(d+)(d{3})/;
while (rgx.test(x1)) {
if(stat=='cz')
x1 = x1.replace(rgx, '$1' + ' ' + '$2'); // nahradime mezerou cesky format 1 000,00
else if(stat=='us')
x1 = x1.replace(rgx, '$1' + ',' + '$2'); // us format 1,000.00
}
return x1 + x2;
}
Letní čas tak připočítat hodinu.
Ověření zda je letní čas.
$dst = date("I"); //I (capital i); 0 or 1 if daylight saving time
// example:
$hour = 14;
if(date("I")===1){
$hour = $hour + 1;
}
// OR
$hour = 14 + date("I");
Styly se nacházejí v příslušné složce temáta, které používáme. Například:
Jednotlivé soubory můžeme upravovat, ale je nutné provádět refresh stylu z Admin centra ACP, záložka Styles -> Style components
Další soubory stylů přidáváme do jako import:
myforum/styles/prosilver/theme/stylesheet.css
Styly můžeme též editovat z ACP tabulka Styles -> Themes a zvolti Edit příslušné šablony
a po uložení provedeme refresh témata.
Nebo Styly - Skiny - Upravit konkrétní šablonu (prosilver atd.)
Někdy musíme vymazat i soubory ze složky cache a ponechat tam jen index.html a .htaccess
mojeforum/styles/prosilver/theme/
Jednotlivé soubory můžeme upravovat, ale je nutné provádět refresh stylu z Admin centra ACP, záložka Styles -> Style components
Další soubory stylů přidáváme do jako import:
myforum/styles/prosilver/theme/stylesheet.css
@import url("prs.css");
Styly můžeme též editovat z ACP tabulka Styles -> Themes a zvolti Edit příslušné šablony
a po uložení provedeme refresh témata.
Nebo Styly - Skiny - Upravit konkrétní šablonu (prosilver atd.)
Někdy musíme vymazat i soubory ze složky cache a ponechat tam jen index.html a .htaccess
Editace: 7.2.2021 - 09:37
Počet článků v kategorii: 77
Url:javascript-js-functions-example