Skip to content

Instantly share code, notes, and snippets.

View ryankinal's full-sized avatar

Ryan Kinal ryankinal

View GitHub Profile
<em>If you haven't read my previous tutorial on <a title="Objects and the Prototype Chain" href="http://blog.javascriptroom.com/2013/01/14/objects-and-the-prototype-chain/">Objects and The Prototype Chain</a>, it would be a good idea to do so. This article builds on the concepts presented there. Object oriented programming with only those concepts, while possible, can get pretty verbose, so we often use common abstraction techniques - called Design Patterns - to simplify the task.</em>
The Initializer Pattern is my favorite OO design pattern in JavaScript. I find it intuitive and easy to use. It's straightforward, and contains all the necessary internal knowledge of an object in the object itself. I'll get to each of these points as we go along, but for now, let's just take a look at the most basic concept of the pattern: an initialization function.
<h2>In Comparison</h2>
In the first article, using basic prototypal features, we see code that looks like this:
[javascript]
var point = {
translate: functi
$(function() {
var $draggingElement = null,
$document = $(document),
$dropA = $('#dropzonea'),
$dropB = $('#dropzoneb'),
$dropCancel = $('#textwordscontainer'),
downHandler = function(e) {
$draggingElement = $(this);
$draggingElement.addClass('dragging').css({
left: e.pageX - $draggingElement.width() / 2,
@ryankinal
ryankinal / useless.js
Created January 23, 2013 13:22
Recursive-descent parser to find the second occurrence of the string 'ABC' in a string.
var aPosition = -1,
position = 0,
count = 0,
desiredCount = 2,
string = 'XYZ 123 ABC 456 ABC 789 ABC',
tokens = string.split('');
var parse = function(tokens)
{
if (tokens[0] === 'A')
@ryankinal
ryankinal / illusion-of-class.html
Created January 29, 2013 17:17
Article on pseudo-classical pattern
<blockquote>This indirection was intended to make the language seem more familiar to classically trained programmers, but failed to do that, as we can see from the very low opinion Java programmers have of JavaScript. JavaScript's constructor pattern did not appeal to the classical crowd. It also obscured JavaScript's true prototypal nature. As a result, there are very few programmers who know how to use the language effectively.
~ Douglas Crockford, <a title="Douglas Crockford - Prototypal Inheritance" href="http://javascript.crockford.com/prototypal.html">"Prototypal Inheritance"</a></blockquote>
The indirection Mr. Crockford refers to here is the <em>Pseudo-Classical</em> <em>Pattern, </em>also known as the <em>Constructor Pattern</em>.<em> </em>This is a way of simulating classical inheritance in JavaScript - a prototypal language.<!--more-->
If it wasn't obvious from both the title of this post, and from the frankly negative quote above, this is not my favorite object-oriented pattern in JavaScript. I'
@ryankinal
ryankinal / blacklisted.md
Created February 1, 2013 14:33
From Arsenal Studios on Facebook

Well, that explains it.... (Why one of our cloud services up and stopped working out of nowhere).

"G'day Jake,

Should be fixed now- please try again. Issue was, we had blacklisted "arse" from being usable in the custom subdomain field (some time after you had chosen one, obviously, and in your case it's perfectly fine!).

Sorry for the inconvenience mate."

@ryankinal
ryankinal / being-programmers.md
Last active December 12, 2015 04:39
Being programmers

A common source of frustration in the web development community is programming done by non-programmers. There is plenty of code out there written by people who have little understanding of data structures, efficiency, and architecture, and that can be frustrating to those of us who have some training in those areas. When we're hired to work with this kind of system, we wonder how the hell it could have gotten to be like that.

Why on earth would someone store all that data in plain text? Why is the login system a flat file? Why is this web service one gigantic file? Why is everything so tightly coupled? This is miserable!

The cause is, often, that the people designing and building the system just didn't have the knowledge necessary to do it well. Maybe databases were foreign to them. Perhaps they had never heard of an event-based architecture, SOLID and DRY, or even taken a class in Object Oriented Programming (or whichever programming paradigm you prefer). And even if there is knowledge of these con

@ryankinal
ryankinal / wtf-strlen.php
Created February 8, 2013 00:28
WTF strlen
<?php
// config file
define('STOP_MESSAGE', ' (txt "STOP" to cancel)');
// form handling
$length = 157 - strlen(STOP_MESSAGE) - strlen($keyword->getAttribute('word'));
$reply = isset($_POST['reply']) ? $_POST['reply'] : false;
if (strlen($reply) <= $length)
{
@ryankinal
ryankinal / feedly-impressions.md
Last active December 15, 2015 04:59
Feedly first impressions

Since Google is killing Reader, I've been trying out Feedly. And on the surface, it looks slick. On top of that, it automatically syncs all my Reader stuff. Awesome feature.

However, there have been some issues.

Configuration

I like to be able to easily scan my list of feeds to see what's new. To facilitate this, I switched almost immediately to Feedly's "Titles" view. I tried this the first time from an on-feed settings menu, and I liked it, so I checked the Preferences page, and found that there was an option to set this view as the default on all feeds and categories. Great! :click:

But it didn't work. I still had the "Magazine" view on a few of my categories. I had to manually set these categories to use Titles. Bah.

@ryankinal
ryankinal / brandon-catch.vb
Created March 21, 2013 14:48
Dammit, Brandon
Sub UserNameValidation(ByVal source As Object, ByVal args As ServerValidateEventArgs)
Try
Dim intUserID As Integer = common.getStudentIDFromUserName(Me.userNameTextBox.Text)
If intUserID <> 0 Then
args.IsValid = False
Else
args.IsValid = True
End If
Catch ex As Exception
@ryankinal
ryankinal / placeholders.js
Created March 27, 2013 14:39
Form placeholders
/* requires jQuery */
$(function() {
var submitting = false;
$('input').focus(function() {
var $this = $(this);
if ($this.data('placeholder') && $.trim(this.value) === $this.data('placeholder'))
{
if ($this.data('type') === 'password')