mhpreiman
12/12/2016 - 9:51 AM

Javascript

Form elements can't have an attached FUNCTION with the same NAME as the form element id!   Eg <input id="arvuta" type="button" onclick="arvuta()"> won't work!


Targeting a bunch of form elements

Nodes

<p id='par'><span>something</span></p>
                                                              //returns differently depending on.. what??
childCount = document.getElementById('par').childNodes        //return NodeList [span]
childCount = document.getElementById('par').childNodes[0]     //return <span>something</span>

New (separate) window element

var nw = window.open();
nw.document.write('separate window');
nw.document.close();


Boolean variable

var a = 4 > 3;   //returns true
var a = 4 + 3;   //returns 7


Var inside strings

console.log(`I want to eat a ${fruit}`);


Template literals (template strings)  

Use ` ` to store larger chunks of html in variables

var myVariable = `
<style>
p {                                  
    font-size: 13px;  
}                                                
p:after {    
    content: "";  
}    
</style>
`;


Evoke function only once

var onetime = alert();


Anonymous function

(function () {

})(); //call it right away


Generator function

function * foo(x) {
    while (true) {
        x = x * 2;
        yield x;
    }
}
console.log(foo(2).next().value);     // return 4


Objects

Use stringify and parse to turn objects into strings and strings into objects.

Interactive Javascript cheatsheet


JS is an OO language but unlike other languages that implement inheritance via classes, JavaScript does so via prototypes.

Properties (and methods) can be added either to the object's instance (eg myimage.size = "26k") or to the object's prototype (eg circle.prototype.pi = 3.142).
Js only allows you to "prototype" prebuilt objects that are created with the new keyword.

How to CREATE an object and ADD methods to it

Also, JS objects are different from other languages' objects: they are a collection of name-value-pairs.



Glossary

Primitive type:  a type of data that represents a single value, not an object.

Operator precedence:  which operator function (+-/*%) gets called first.            
list of 20 operators (see Table)

Associativity:  the order how (left→right vs right→left) operators with the same precedence get called (jagan-korrutan, siis liidan-lahutan).

var a = 2, b = 3, c = 4;
a = b = c;     // associativity of = is R→L  :  c (value 4) → b → a

console.log(a, b, c);     // 4 4 4

Coercion:  converting a value from one type to another.