javascript

Default function parameters in javascript

Snippet
// pre ES6, test inside the function call
function go_fish(a, b) {
  var b = (typeof b !== 'undefined') ?  b : 1;
  // ...
}
 
// ES6, assign in function call
function go_fish(a, b = 1) {
}

Principles of Object Oriented JavaScript

Book Notes

As part of my belief that one should never stop learning, I keep trying to learn. This month's learning involved reading this book, Principles of Object Oriented JavaScript, a relatively short book by Nicholas Zakas.

I'm comfortable in JavaScript, but, really, haven't ever taken the time to read about some of the insides of the language. I'm glad I picked up this book.

This book is a quick read, about 132 pages of content and 25-ish of index, depending on the format you read the book and the font size you select. The book covers primitive types, reference types, built-in types, function declarations vs function expressions, objects in general, property attributes, object constructors, object prototypes, property attributes, inheritance and various object patterns. The code examples are clear, the writing voice conversational.

I had a couple "Ohhhhhhhhh!" moments, which were fun, with a lot of head-nodding-yep-I-knew-that moments, too.

I liked the book. Recommended.

HTML from a JavaScript rendered page

Snippet

Saving a page source with javascript rendered elements means you get the script tags, but not the rendered page.

Barring a browser plugin, this'll do.

/* display in console */
console.log(document.getElementsByTagName('html')[0].innerHTML);
 
/* or copy to the clipboard */
copy(document.body.innerHTML);

One liner for swapping vars in Javascript

Snippet

You can swap two variables in javascript like every other language: by using an intermediate variable. OR! You can have a one liner.

Love me those one liners!

b = [a, a = b][0];

safely coercing strings to numbers with javascript

Snippet

safely coercing strings to numbers) #javascript

return (+value + '') === value ? +value : value;

javascript using string of function name to call function

Snippet

Have a string of a function name and want to call the function whose name is the string.

// Consider:
 
var searchFn = "fnCallBack";
 
// want to do this (which fails)
 
searchFn(name);
 
// use this:
 
eval(searchFn)(name);
 
// or this:
 
var fn = new Function("term", "return " + searchFn + "(term);");
fn(name);
 
// be calling this:
 
fnCallBack(name);

Check if a jquery object has a particular class/id

Snippet

Use is() to check if a class exists on a jQuery element.

if ($(#elm).is('.checkthisclass')) { 
 // #elm has the class
} else { 
 //#elm doesn't have the class
}

PHP empty in javascript

Snippet

Equivalent of PHP's empty in javascript

function empty (mixed_var) {
 // version: 909.322
 // discuss at: http://phpjs.org/functions/empty
 var key;
 if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || mixed_var === undefined ) {
  return true;
 }
 if (typeof mixed_var == 'object') {
  for (key in mixed_var) {
   return false;
  }
  return true;
 }
 return false;
}

Parse JSON from server in javascript

Snippet

When a server response (say, from an ajax call) returns JSON formatted code, eval to bring the results into the script's space. Don't use with any returned JSON object that you don't really really really trust.

/*
 server returns 
 {"a":1,"b":2,"c":3,"d":4,"e":5}
 */
 
// NEVER DO THIS
var obj = eval('(' + jsontext + ')');
alert(obj.a);
// will receive "1" in alert (no quotes)
 
// DO THIS
JSON.parse( json.data );

Worse for wear

Blog

I went over to Google today for a javascript meetup. I've been trying to be more active and involved in the various communities, groups and events that living in the Bay Area offers. This event was one in a small series of potentially interesting topics. I arrived, parked the car, and wandered around to the front of the building, not really sure where I was going, having only the building number.

When I arrived at the front of the building, the main door had a key pad on it. Okay, that wasn't the right door, so I asked the guy standing next to the door, with a key card in his hand. "Hi, I'm here for a javascript meetup. I'm supposed to go into this building, but I see this door requires a key card, and the lobby is dark. Can you help me?"

"Uh...."

"Could you suggest where I can try next to get into this building?"

"Uh, no?"

A little stupified, and more than a little annoyed, I asked, "Is there another door to this building, or is this the only door available?"

"Oh, yeah, around the corner," he answered, pointing to my left.

"Thanks," I offered, and started walking away, completely amazed that the company that supposedly hires the best and the brightest clearly missed the mark with some of its employees. Maybe lessons in "common sense" should be on their 20% agenda.

So, I wandered around to the next door, and found it happily open, with the lights streaming into the lobby and a small line of people waiting to sign in. I stood in line and, when I was second in line behind the guy entering in his information, I tried to see what he was typing so that I knew what to expect. I missed most of his work, as he shifted his body over to prevent my viewing the screen.

Because, you know, I wouldn't be able to read his nametag on his chest after he signed in.

So, when it was my turn, and the second page of options had more than one perfectly valid answer, I asked him, "What did you mark for the reason for the visit?" he threw up his hands and yelled at me, "OH I DON'T KNOW!"

Everyone in the lobby turned to look at us. I smiled. "Well, then, I'll just put down social visit here." He walked away.

Google is looking a little tired these days, I have to say. The energy and excitement of the place is totally gone. The enthusiasm of the campus, once so overwhelming, was absent, too. I can't say I felt any of the tech love that just oozed from the buildings. Instead, the place felt cold and down. Even the seats in the auditorium we were in are, well, a lot worn:

IMG_0770.JPG

Of course, that could have been because I had people yelling at me and staring at me. The first not so enjoyable. The second can be entertaining, appropriately applied.

The actual meetup was a little weird, too. About 10 minutes into the first presentation, which wasn't the expected first talk, a group of four Asian kids walked into the room. Okay, not kids per se, they were in their early 20s or so. They couldn't have been too old, as a few moments later, a girl walked in, clearly knowing the other four. Close to her hip, an an old woman walked in behind her. The girl quickly sat down in the last empty seat in the first row, next to the four guys, leaving the old woman to wander and find her own seat. A few moments later, an even older guy came in and spent the next 10 minutes wandering back and forth and back and forth in the rows looking for a seat.

IMG_0768.JPG

I couldn't figure out if these two really old people were really interested in javascript. More power to them, if they are. if they aren't, good lord, you ignornant, disrepsectful, asshole kids, find f---ing seats for your parents.

The talks themselves were, unfortunately, not what I was interested in hearing about. Although I was impressed somewhat with the first talk, and unsure of the point of the second talk, the third talk was fun, but not as applicable to my project as I had hoped it would be. I think my next JS approach is going to be a post to the list asking for help instead of trying face to face.