JavaScript-Mancy by @vintharas at #DevSum17 @DevSum_swe

I had the great honour to take part in Jaime González García's afternoon dance-and-AngularFire session at this year's Developer Summit and was lucky enough to win his book Getting Started With the Arcane Art of Writing Awesome JavaScript for C# Developers. The book is great! Jaime succeeds in entertaining you (well ... me ;-)) throughout the entire book by taking you on a fantasy adventure as he is teaching you new things (such as the spread operator, Maps and Sets) in ES6 and their equivialent in C# and the, slightly aged, ES5.

Getting Started With the Arcane Art of Writing Awesome JavaScript for C# Developers
Now, I'm well versed in ES5, but still - in the first part of the book, I learned something new - while having fun! Oh, and ... check this out:


Uh-huh! It's in the book! Get it now!

While reading the book, I found this memory rule for myself - when iterating in JavaScript, for ... in yields the Index (or key), while for ... of yields the Object. Also, Jaime tought me about different strategies for polymorphic functions, which made me think about memoization strategies (i.e. a function retaining state (cache), to quicker respond to frequent calls).

function dealWithNumbers(message) {
    if (typeof message === "number") {
        this.value = this.value || 0;
        this.value += message;
        return this.value;
    }
}

function dealWithStrings(message) {
    if (typeof message === "string") {
        return `Well, hello there, ${message}! Pleased to meet you`;
    }
}

function dispatch(message, ...functions) {
    for (let f of functions) {
        let value = f.call(f, message);
        if (value) return value;
    }
}

dispatch(10, dealWithNumbers, dealWithStrings);
// 10

dispatch(10, dealWithNumbers, dealWithStrings);
// 20

dispatch('Sami', dealWithNumbers, dealWithStrings);
// "Well, hello there, Sami! Pleased to meet you"

Just lovely :-)

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints