Monday, December 12, 2016

Mapping JS

Declare
const testMap = new Map()

set map
testMap.set(1,{name:"boris",cash:345});
testMap.set(2,{name:"tony",cash:45});
testMap.set(3,{name:"peter",cash:5});
Map {1 => Object {name: "boris", cash: 345}, 2 => Object {name: "tony", cash: 45}, 3 => Object {name: "peter", cash: 5}}
properties
testMap.size
3
testMap.has(2)
true
testMap.has(56)
false
methods
testMap.get(3).name
"peter"

for loop
//first item in array is key and second is value
for(let item of testMap){ console.log( item[1].cash)}//value
345
45
5
for(let item of testMap.keys()){ console.log( item)}
1
2

3

Friday, December 9, 2016

Array methods

Using splice to add items in any position

const arr = ['a','b','c','d']
x = arr.splice(2,0,'3')
x will be empty - []
arr will be ["a", "b", "3", "c", "d"]

Add to the beginning of array

arr.unshift(-13)
[-13, "a", "b", "3", "c", "d"]

remove 1st item

arr.shift()
["a", "b", "3", "c", "d"]

For Each

var temp =””
arr.forEach(function (item) {
    if (isNaN(item)) {
        temp += item;
    }
});
temp = "abcd"

mapping

const test =[{name:"herby",cash:23},{name:"boris",cash:456} ]
const cash = test.map(x=>x.cash)
cash = [23, 456]

Every and Some

const test2 =[{name:"herby",cash:23,eyecolor:"brown"},{name:"boris",cash:456} ]
test2.some(x=>x.eyecolor)
true
test2.every(x=>x.eyecolor)
false

Find

returns 1st item
const z = test2.find(x=>{return x.eyecolor === "brown"})
z    {name:"herby",cash:23,eyecolor:"brown"}
const w = test2.find(x=>{return x.cash  > 0})
w   {name:"herby",cash:23,eyecolor:"brown"}

to get multiple items as an array use:

Filter

const t = test2.filter(x=>{return x.cash  > 0})

sort

 ascending

test2.sort((x,z)=>x.cash -z.cash)

descending

test2.sort((x,z)=>z.cash -x.cash)

Aggregating

console.log(test2.reduce((a,c)=>a+=c.cash,0))

479

Tuesday, December 6, 2016

why is 32 bit x86

here

New Parameter Functionality in JS

  1. New Parameter Functionality

    Rest Params

    function testParams( i , ...j){
    var total = 0;
    j.forEach( function(value, index) { total += value; });
    return Math.pow(total,i);
    }
    testParams(2,1,2,3,4)
    100

    Rest Params with Arrow Functions
    function testParams( i , ...j){
    var total = 0;
    j.forEach( value => total += value);
    return Math.pow(total,i);

    }
    testParams(2,1,2,3,4)
    100

    default Params

    function testDefault(i, power =2){
    return Math.pow(i,power );
    }

    testDefault(10)
    100
    testDefault(10,3)
    1000

access list boxes - stop selection

use Locked property


if you use the enabled property then the scrollbars wont work

Monday, December 5, 2016

const in js

  1. new const feature

    in the past there was no real constants in js, everything could be overwritten (even sometimes keywords)
    in ECMAScript6 this has been alleviated

    const in JS means the object can be assigned once
    if you try this

    const i = 45
    i= 0
    console.log(I)



    this will fail
    SyntaxError: Identifier 'i' has already been declared

    with objects member can be assigned afterwards


    const obj2 = {}
    obj2.a ="gg"



    but subsequent
    obj2 = {}
    will fail

    even with const, the only way to have a readonly member of an object is

    var testobj = {};
    Object.defineProperty( testobj , "pseudoconst",
    {
    value: 3,

    writable: false,

    enumerable: true,

    configurable: true
    });


    testobj.pseudoconst
    3
    testobj.pseudoconst = 5//this wont raise an error but wont change the value either
    5
    testobj.pseudoconst
    3

let in js

  1. as Douglas Crockford has said, the worst feature of JavaScript is global variables.
    This has been alleviated greatly by block scoped variables

    consider the following


    var test ="herman"

    {
    test ="boris";
    console.log(test)
    }
    console.log(test)

    or


    var test ="herman"

    {
    var test ="boris";
    console.log(test)
    }
    console.log(test)

    the result will be


    boris
    boris

    However, with the new let keyword

    var test ="herman"

    {
    let test ="boris";
    console.log(test)
    }
    console.log(test)

    the results are now
    boris
    herman