I’m in the Alt.net podcast on jQuery
Mike Moore was kind enough to invite me on the alt.net podcast to talk about the recent jQuery announcement by Microsoft. Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Steven Harman were also on the podcast.
This was a great discussion. It was especially nice to have Bertrand during the discussion. If we had any questions about how or why Microsoft did something, we could ask him. I was happy to hear that one of the reasons they chose jQuery was because they didn’t want to write their own CSS selector functions for MS AJAX. It’s nice to see that Microsoft sees the benefit in not always reinventing the wheel. Now, any new features or improvements that Microsoft would have made to their own selector engine can be contributed towards jQuery, making it a better product for all of us.
Modern JavaScript Development: Scope
After hearing an interview Scott Hanselman did with Scott Cate about JavaScript a while ago, I decided to use one of my presentations into a screen cast. In this two-part screen cast, I cover how scope works in JavaScript and how we can use object literal notation to fake namespaces.
Making JavaScript Less Painful part 1
Making JavaScript Less Painful part 2
I’m still finding my way around screencasts. Eventually I’ll upload these to vimeo, screencasts.com,, and youtube as well as making them embeddable and providing embed code.
Herding Code #17
We’ve gotten a lot of great feedback about our podcast. Despite that, we’ve continued recording them and have made it to episode 17 – Browser Roundup.
There has been a lot of activity in the browser and JavaScript arena. IE 8 beta 2, Chrome, FireFox 3.1 beta, TraceMonkey, V8, SquirrelFish,etc… And most of us on the podcast are web developers at some point or another during our day jobs. We had to cover it. You’ll get to hear me misrepresent what .NIB files are and mispronounce OS X.
There is also a good description on the Chromium blog about how Chrome utilizes multiple processes.
Modern JavaScript Development: Reflection in JavaScrpt
Sometimes you want to find out what members an object exposes. There's a pretty simple way to do this. A simple for-in statement.
-
for(var member in obj){ alert(member); };
No really, that's it. No "imports" or "using" statments. No complicated classes to memorize, no unwrapping or casting. Just for-in over an object. One caveat, this will show you ALL of the objects members and it doesn't distinguish between members declared directly on the objects and members inherited through the objects prototype. What if you just want to find the methods on an object? I've created two helper functions that will return just the methods(functions) of any JavaScript object.
-
Object.prototype.getAllMethods = function(){
-
var memberArray = new Array();
-
for (var method in this) {
-
if (typeof this[method] == 'function') {
-
memberArray.push(method);
-
};
-
};
-
return memberArray;
-
};
-
-
Object.prototype.getOwnMethods = function(){
-
var memberArray = new Array();
-
for (var method in this) {
-
if (typeof this[method] == 'function' && this.hasOwnProperty(method)) {
-
memberArray.push(method);
-
};
-
};
-
return memberArray;
-
};
In the getOwnMethods function we use the hasOwnProperty function to discriminate between methods declared in the object and methods inherited through the prototype.
Great JavaScript presentation by John Resig
John Resig, of JQuery fame, gave a great presentation on JavaScript at Northeastern University.
Modern JavaScript Development: null and undefined
In my last post on constructors and objects in JavaScript, I mentioned that the logical OR operator || was short-circuited and allowed us to set default values for arguments passed into the constructor. So we know that the argument evaluates to false if we don't pass it in to the constructor, but what value does the argument have that makes it evaluate to false?
There are three different values, or types, that a variable can have in JavaScript, null, undefined, and the value you set.
-
var isNull = null; //null type
-
var isUndefined; //undefined
-
var isNumber = 1; //neither null nor undefined, contains a value
A variable is null if you explicitly set it to the null value or if it receives a value from an expression that results in null; A variable is undefined if you declare it and haven't assigned a value to it yet. The tricky part about null and undefined is that they are equal, but are two separate types. Consider the following code.
-
var isNull = null;
-
var isUndefined;
-
alert(isNull == isUndefined);
So how do we differentiate between null and undefined variables? By using either the typeof function or the identity(===) operator. The identity operator has an evil twin (!==) that returns true if the object is not of the type you passed. The typeof function is defined in the JavaScript global object and returns a string containing the name of the type of object passed to it. The identity operator is a binary operator that compares the types of the two objects being compared and returns a boolean.
-
var isNull = null;
-
var isUndefined;
-
alert(isNull == isUndefined);
-
alert(isNull === isUndefined);
n.b. If you pass "null" as an argument to a function, typeof will return "Object", but the object will === null.
-
function defaultArgs(name, options) {
-
alert(typeof(name));
-
alert(name === null);
-
alert(name === undefined);
-
this.name = name || "unknown";
-
this.options = options || { size: 42 };
-
};
-
var instanceOne = new defaultArgs("Scott");
-
var instanceTwo = new defaultArgs();
-
var instanceThree = new defaultArgs(null, {size : 43 });
So, how does this affect the logical OR operator? Well, both null and undefined are evaluated to "false" by the || operator, but the argument is still declared in the scope of the function and you can assign a default value to it. It's important to know the distinction between null and undefined. A function call may result in a null return value, a failed function call may result in an undefined value.


