Excellent Javascript/jQuery resources

Lexical scoping/closure
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

Understanding Scope and Context in JavaScript

this

Demystifying JavaScript Variable Scope and Hoisting

Event Capturing and Bubbling in JavaScript
Bubbling and capturing
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events

How JavaScript Event Delegation Works

JavaScript Prototype in Plain Language

JavaScript Promises for Dummies

Eric Elliott’s “Master the JavaScript Interview” Series

Anjana Vakil: Learning Functional Programming with JavaScript – JSUnconf 2016

How JavaScript Event Delegation Works

Event order

Closures in loops – Javascript Gotchas

Your JS is a Mess. Javascript Namespacing

Sticky vs Non-Sticky sessions

A good explanation on sticky vs. non-sticky sessions:

When your website is served by only 1 web server, for each pair, a session object is created and remains in the memory of the web server. All the requests from the client go to this web server and update this session object. If some data needs to be stored in the session object over the period of interaction, it is stored in this session object and stays there as long as the session exists.

However, if your website is served by multiple web servers which sit behind a load balancer, the load balancer decides which actual (physical) web-server should each request go to. For example, if there are 3 webservers A, B and C behind the load balancer, it is possible that www.mywebsite.com/index.jsp is served from server A, www.mywebsite.com/login.jsp is served from server B and www.mywebsite.com/accoutdetails.php are served from server C.

Now, if the requests are being served from (physically) 3 different servers, each server has created a session object for you and because these session objects sit on 3 independent boxes, there’s no direct way of one knowing what is there in the session object of the other. In order to synchronize between these server sessions, you may have to write/read the session data into a layer which is common to all – like a DB. Now writing and reading data to/from a db for this use-case may not be a good idea. Now, here comes the role of sticky-session. If the load balancer is instructed to use sticky sessions, all of your interactions will happen with the same physical server, even though other servers are present. Thus, your session object will be the same throughout your entire interaction with this website.

To summarize, In case of Sticky Sessions, all your requests will be directed to the same physical web server while in case of a non-sticky loadbalancer may choose any webserver to serve your requests.

From https://stackoverflow.com/questions/10494431/sticky-and-non-sticky-sessions

Javascript’s Reduce method

The Reduce method reduces an array into a single value by applying a callback function to each element in the array, starting with the leftmost to the rightmost element.

As a simple example, we could use reduce() to find the sum of all the numbers in an array.

const numbers = [3, 2, 5, 7, 8, 10];
const sum = numbers.reduce(function (total, item) {
  return total + item;
});
// Returns 35

In ES6 syntax,

const numbers = [3, 2, 5, 7, 8, 10];
const sum = numbers.reduce((total, item) => total + item);
// Returns 35

More great examples of the Reduce method:
https://medium.freecodecamp.org/reduce-f47a7da511a9

Full documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Installing Maven plugins

To download plugins and the required dependencies, run this command:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.6:get -Dartifact=groupId:artifactId:version

For example, to download versions-maven-plugin:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.6:get -Dartifact=org.codehaus.mojo:versions-maven-plugin:2.3 -DrepoUrl=https://github.com/mojohaus/versions-maven-plugin

If the plugins need to be installed manually, refer to this documentation on the Apache Maven project site.

Installing Apache Maven on El Capitan

  • Add the following line to ~/.profile or ~/.bash_profile:
export JAVA_HOME=$(/usr/libexec/java_home)

To verify the JAVA_HOME environment variable has been set correctly, type:

echo $JAVA_HOME

which should return something like this:

/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home
  • Verify that JDK 7+ is installed.  Type the following in a terminal window:
java -version

It should give you something like this:


java version "1.8.0_102"

Java(TM) SE Runtime Environment (build 1.8.0_102-b14)

Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

(At the time this was written, the version was 3.3.9)

  • Extract the Maven Archive
  • Type the following lines in terminal:

sudo su

mv [apache-maven-directory]/apache-maven-3.3.9 ~/opt

exit

 

where [apache-maven-directory] is the path of the directory where you extract the maven archive in.

  • Add Maven binaries to user path.  Add the following line in ~/.bash_profile

export PATH=/opt/apache-maven-3.3.9/bin:$PATH

save and quit the file, then execute .bash_profile again.

  • Verify Maven installation by typing:
mvn -version

which should return something like this:


Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)

Maven home: /opt/apache-maven-3.3.9

Java version: 1.8.0_102, vendor: Oracle Corporation

Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

Installing Apache Maven on Mac

While the installation guide on Apache is helpful, I decided to document the exact steps (some are not covered on the Apache site) I took to get Apache Maven installed in my machine.

  1. Add JAVA_HOME environment variable to .bash_profile

    export JAVA_HOME=$(/usr/libexec/java_home)
  2. Reload .bash_profile by running:

    . ~/.bash_profile
  3. Download the Apache Maven distribution archive from here.
  4. Add the bin directory to the PATH environment variable.
    In, .bashrc, add:

    export PATH=/path/to/apache/maven-3.3.9/bin:$PATH
  5. Reload .bashrc by running:

    . ~/bashrc
  6. Type the following the confirm Maven has been installed successfully:

    mvn -v

What are PHP traits?

Traits are a group of functions that can be included in other classes. For example,

trait A {
    public function test() {
        print 'HELLO';
    }
}

class B {
    use A;
}

class C {
    use A;
}

$b = new B();
$b->test(); // Will print 'Hello'.

$c = new C();
$c->test(); // Will print 'Hello'.

What is a mixin?

Adapted from Wikipedia,

… a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes. …

… Mixins encourage code reuse and can be used to avoid the inheritance ambiguity that multiple inheritance can cause, or to work around lack of support for multiple inheritance in a language. A mixin can also be viewed as an interface with implemented methods.

Syntax differences between Python and PHP

As a PHP developer for over ten years, I can’t help by compare its syntax with Python when I started learning the latter recently. I anticipate this page will be a perpetual work in progress…

Variable names

PHP – ‘$’ in front of variable names.  E.g.,

$str = 'abc';

Python – No ‘$’ in front of variable names.  E.g.,

str = 'abc'

 

Semicolons

PHP – Must have a semicolon to end each line of code. E.g.,

$str = 'abc';</pre>
<pre>$str2 = 'def';</pre>
<pre>

Python – Each line must be ended with a newline.  E.g.,

str = 'abc'
str2 = 'def'

 

‘If’ statements

PHP

if ($city == "Charlotte"):
  return 183;
elseif ($city == "Tampa"):
  return 220;
else:
  return 475;
endif;

Python

– IMPORTANT: The indent must be 4 spaces.

if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    else:
        return 475

 

Functions

PHP

function render($arg = NULL) {
}

Python

def render(arg):

Arrays in PHP = Lists in Python

PHP

$array = ['a', 2, 'c'];

Python

array = ['a', 2, 'c']

Associative arrays in PHP = Dictionaries in Python

PHP

$array = [
    "foo" => "bar",
    "bar" => ['flint', 'twine', 'gemstone'],
];

Python (Syntax is very similar to JSON)

array = {
    'foo' : 'bar',
    'bar' : ['flint', 'twine', 'gemstone']
}

For loop

PHP

foreach ($arr as $value) {
}

Python

for value in arr:

PHP’s implode = Python’s join

PHP

$s = "-";
$seq = ("a", "b", "c"); # This is sequence of strings.
print implode($s, $seq); # Will print a-b-c

Python

s = "-"
seq = ("a", "b", "c") # This is sequence of strings.
print s.join( seq ) # Will print a-b-c

& operator

Constructors

PHP

function __construct()

Python

def __init__(self)

Inheritance
PHP

class Bar extends Foo

Python

class Bar(Foo)

Parent class
PHP

parent::foo()

Python
super(bar(), self).foo()

Deleting multiple git branches

Here’s something new I learned today. I was trying to clean up some stale branches but Tower only deletes one at a time even if you selected multiple of them! Well, that’s annoying. So I started searching on the internet and this seems to work:

  1. List all the branches that have the ‘PREFIX’ prefix. This is just to preview the list before deleting it.
    $ git branch -r | awk -F/ '/\/PREFIX/{print $2}'

    Note: -r shows all the remote branches. -a shows both local and remote.

  2. If it looks good, delete away! (Scroll to see the command as it is pretty long)
    $ git branch -r | awk -F/ '/\/PREFIX/{print $2}' | xargs -I {} git push origin :{}

    Note: As of Git v1.5.0, you can also delete a remote branch using:

    $ git push origin --delete <branchname>
  3. Remove local reference to those remote branches.
    $ git remote prune origin

    or

    $ git branch -D <branch1> <branch2> <branch3>