By

Tales from the gist, Part II: me.hasOwnProperty('forgetful')

This is Part II of a mini-series discussing Rebecca Murphey's gist challenge for javascript developers. See Part I for more details.

me.hasOwnProperty('forgetful');

That statement returns true, in case you were wondering. That's because I failed to use this method for at least 2 of the challenges. If you read my previous post, you can see I left this out on question #4. That was not a big deal (it wasn't really required), but #3 is a different story:

// 3: given the following code, how would you override the value of the bar
// property for the variable foo without affecting the value of the bar
// property for the variable bim? how would you affect the value of the bar
// property for both foo and bim? how would you add a method to foo and bim to
// console.log the value of each object's bar property? how would you tell if
// the object's bar property had been overridden for the particular object?
var Thinger = function() {
  return this;
};

Thinger.prototype = {
  bar : 'baz'
};

var foo = new Thinger(),
    bim = new Thinger();

To which my response was:

// original answers
foo.bar = 'new'; // part 1
Thinger.prototype.bar = 'newer'; // part 2

// part 3
Thinger.prototype.logValue = function() {
      console.log(this.bar);
}
// part 4
Thinger.prototype.propertyChanged = function(propName) {
      return this[propName] !== Thinger.prototype[propName];
}

Everything looks okay (I hope), until you get to part 4, where I created a Thinger.prototype.propertyChanged method... Yup. I created a method that already exists in the core language. Awesome. This was a much more egregious omission of hasOwnProperty(). But now that we're in-the-know, let's update part 4 to use our new method-friend:

// part 4
foo.hasOwnProperty('bar');
bim.hasOwnProperty('bar');

Much better.

The problem here wasn't pure ignorance. I knew what I wanted to do, and I definitely knew the method existed (I think it was covered in Javascript: The Good Parts), but I just completely forgot to use it in crunch time. Hopefully doing exercises like this will help me get this "under my fingers"* and be just a little bit better next time.

  • props to Mr. Cianci for pointing this talk out to me