Nodejs module object and modules in general

The module object in nodejs is what I often use when creating modules in nodejs. The module exports property of the module object is what I use to return a main function that will be called when using the module elsewhere. In addition the main function that I exports with the module exports propriety can have additional properties attached to it which in many respects makes it a better option to the exports global. So then I generally thing that exports property of the module object is a good way to set public methods and properties for a module that I might be making for a nodejs project.

Read More

JavaScript fizzbuzz examples

When looking for code examples that solve a given problem many of us might just seek out something that just works often on stack overflow, copy and past it in, and move on. Although there might be many great code examples for certain problems out in the open web that work fine, they might not always work great all the time, in every little way.

In this post I will be going over not one, but many JavaScript examples of a solution to a simple game of sorts know as fizzbuzz. The you tube user Tom Scott did a video of fizzbuzz on youtube that I managed to catch that inspired me to write this post, and expand on my collection of javaScript example posts that I would like to keep working on a little now and then.

Tom brings up a lot of important points that I try to keep in mind when writing code, not just for the sake of this post but in general. There is of course just quickly throwing together something that works, and then there is thinking more in terms of readability, and preparing for the future. Getting together a few code examples on a simple programing things such as fizz buzz might help to get a better idea of the fact that just because something works it does not meed that there is something wrong with it.

Read More

JavaScript mouse topics beyond just the events

In client side javaScript mouse events are a way to get a mouse cursor position as well as the state of one or more mouse buttons. The javaScript mouse events are a collection of several types of events that can be attached to the window object, or just about any html element with the add event listener method.

There are three general event types of interest when working something out with mouse events that are on mouse down, on mouse move, and on mouse up. There are other events of interest, such as mouse out and mouse over events, but for the most part I only bother with those first three events when working out any kind of user interface that will make use of a mouse if present.

Mouse events are not always the best way to go about making a truly universal input controller type module, or any kind of component of an application that has to do with user input, at least not by them selfs anyway. There are of course also touch events, and keyboard events that should be taken into consideration also when working on something to that effect. So then when it comes to working out an interface that will work with pointer devices in general it might be better to start out with pointer events actually. Pointer events are as the same suggests about pointers in general not just in terms of a mouse, but touch and pen devices also. There is then only bothering with mouse and touch events when it comes to making separate user interfaces for desktop and mobile devices.

However mouse events work great when it comes to attaching events that will allow for a great deal of control over what will happen for traditional desktop clients. When doing so it is impotent to be aware of how browsers simulate mouse events for touch scripts, and how to prevent that default behavior. It should also go without saying that one should also use touch events, and or pointer events as a fall back for mouse events to make a user interface work with all clients that will visit a page. In any case in this post I will be covering a great deal about mouse events, but also pointer events in general while I am at it.

Read More

document querySelector method and other ways of getting references to elements in javaScript

In late specs of client side javaScipt there is now the document.querySelector method as well as another method called document.querySelectorAll in the document object in client side javaScript. The query selector method can be used to get a single element by way of an id, class name, or tag name. The query selector all method works in a similar way but can be used to get a collection of elements rather than just one. So these methods are yet another way to go about getting a reference to a single element, or an NodeList that is a kind of array of elements.

For the most part using this method is safe as of this writing, unless for some reason you want to have support for older browsers that are not used that often any more. There are other options that I find myself still using just for the hell of it, for example if I do just want to get an element by id then I still find myself using document.getElementById to do so. Still the querySelector method works well at getting at an element not just by id t, but also in an array of different ways other then that of just an id property value of an element.

I thing that it is not a good idea to get caught up in this nit puck issues though, regardless if query selector is used, or a more tired yet true option, the end result is the same. One way or another I want to gain a reference to an element, or a collection of elements.

Read More

javaScript and pure functions

So in javaScript there are many different kinds of functions such as function expressions, declarations, and arrow functions. However there is also many different ways how functions can be used, inside the body of a function it is possible from me to change the value of something that is outside of the functions scope, it is also possible to write functions where I do everything I can to avoid doing something like that. There is creating a local variable inside the body of a function that has a value that is generated by something like the Math.random method, and then there is making that local variable an argument of the function that can have a value created by Math.random, or a fixed set value.

There is the nature of constructor functions and how they are used as a way to create functions that are called off of an instance of that constructor by way of a prototype object. In contrast to that of a constructor function there is what many might call a pure function, or at least a pure like function if I can call it that, which might be a function that has at least some of the features of a pure function. In pure functions one of the features is that a pure function will always return the same result for the same arguments, this is not always the case when calling the prototype method of a constructor instance as the value can change from one call to the next with the same arguments depending on the value of the state.

With the instances of constructor functions there are prototype methods like that of the array splice method for example that will mutate an array in place, and then there are other methods in the array prototype such as slice that will not mutate the source array, but instead return a new array without mutating the source array off of while array slice was called. However even with array slice it can still end up giving different results depending on the state of the array that is is called off of, so although it is maybe a pure like function it is not a true pure function going by a strict definition of what one is.

So in this post I will be going over some pure functions, or at least functions that demonstrate some of the features of pure functions. I would say that using pure functions helps to keep things a little more organized, easy to read, and debug, but it is not enough for me to just say that. It would be best to see for ones self, not just by reading content such as this, but by taking the time to work out ones own pure function examples. Still it is nice to have some kind of guide as a starting point so lets get to it.

Read More