Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

javascript js functions like old class example not ECMAScript 2015 ES6


AD MOB


// 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:14

77LW NO topic_id




AD

Další témata ....(Topics)


70

PHP Měření času microtime | php-mereni-casu


Jak změřit čas trvání vykonání nějaké funkce, nebo provedení části kódu v PHP


$start_time = microtime(true);

// do some work

$end_time = microtime(true);
// Calculate  execution time
$execution_time = ($end_time - $start_time);

echo " Execution time = ".$execution_time." sec";

Date: 11.06.2020 - 09:14
19

Slovník podstatných jmen | slovnik-podstatnych-jmen


Jak vytvořit slovník podstatných jmen.

Velmi snadno. Stačí si stáhnout slovník pro kontrolu pravopisu
do Open Office org a tam již druhy slov májí za lomítkem určující znaky.

//wiki.services.openoffice.org/wiki/Dictionaries#Czech_.28Czech_Republic.29

Ukázka obsahu slovníku:

alokuce/Z
alokující/YN
alokátor/H
alolex/H
alomorf/H
aloový/YKRN
aloparentální/YKR
alopatický/YCRN
alopatie/Z


Dodržujte licence slovníků
39

PHP int to char to int php | php-nt-to-char-to-int-php


int to char, char to int, string to int php example source code.

Jak konvertovat číslo na znak a znak na číslo v php:


Ke konverzi čísla na znak slouží v php funkce chr() viz příklad:


for($i=65;$i<91;$i++){

print chr($i); // print upercase of alphabet A - Z


}


Ke konverzi znaku na číslo slouží v php funkce ord()


print ord("A"); // 65

Konverze pomocí přetypování string na int, nebo na float v php:

$int = (int)"bla123a";
print $int; // 0

$int = (int)"123bla";
print $int; // 123

$int = (int)"12.3bla";
print $int; // 12

$float = (float)"3.14pi";
print $float; // 3.14

is_numeric example source code



if (is_numeric (5974)) echo "is numeric";
else echo "no numeric";

Is number odd or even - je cislo liche nebo sude php example



if((11 % 2)==0) echo "number is even";
else echo "number is odd";

33

PHP php global | php-global


global je klíčové slovo, které zajistí viditelnost proměnné například uvnitř funkcí.

$a = 5;
$b = 2;

function Secti()
{
    global $a, $b;
    $b = $a + $b;
} 

Secti();
echo $b;// 7

// pred vsemi include file
$varGlob = 10;
include('some.inc');

$GLOBALS; // pole ktere obsahuje SERVER i vsechny uzivatelovi globalni promenne

function PrintFC()
{
   $a = $GLOBALS['varGlob']; // ziskame globalni promennou
   $GLOBALS['varGlob'] = 20; // muzeme prepsat jeji hodnotu 
   print $a; // 10
} 

PrintFC();
print $varGlob; // 20

45

javascript DOM methods | javascript-dom-methods


Tohle jsou některé metody objektu document


clear
createDocument
createDocumentFragment
createElement
createEvent
createEventObject
createRange
createTextNode

getElementById
/* nejpouzivanejsi metoda objektu document, ktera
ziska handle elementu dle jeho id*/

getElementByTagName
var e = document.getElementById('list');
e= e.getElementsByTagName('span');
i=0;
while(i < e.length){ 
 e[i].style.color = "green";
}

write
document.write("some text");







Editace: 7.2.2021 - 09:37
Počet článků v kategorii: 77
Url:javascript-js-functions-example

AD
11 Share
Share
Tweet