For whatever reason, it was decided that instead of doing another Ruby sprint, we will do a review sprint instead. I was totally unprepared for this. Anyway, let’s just face it.
About 7 students were getting ready for the upcoming Quora contest on machine learning. I didn’t participate since I would be out of town on the day of contest. Oh, well…
The master says, get back to work. Yes, master. I finished the regex sprint and implemented a prefix tree. My data structure skills are kind of average and totally needs more attention. I end up pairing with Al on the prefix tree and he is a great pair to work with 🙂
Subclassing in JavaScript
// In JS, method is a free agent, unlike some other languages var obj ={ cat: 'mii', cow: 'muu' }; var shout = function(input){ console.log(this[input]); }; obj.shout = shout; obj.shout('cat'); //or we can call the function in the context of 'obj' shout.call(obj, 'cat');
As Larry said, this seems to be irrelevant to subclassing, but it is indeed very relevant.
var Phone = function(color){ this.color = color; }; Phone.prototype.changeColor = function(newColor){ this.color = newColor; console.log("Phone has a new color: ", this.color); }; nokiaPhone = new Phone('black'); nokiaPhone.changeColor('grey'); //now subclassing var SmartPhone = function(color, screenSize){ Phone.call(this,color); // see the similarity? this.screenSize = screenSize; }; SmartPhone.prototype = Object.create(Phone.prototype); SmartPhone.prototype.constructor = SmartPhone; SmartPhone.prototype.changeColor = function(newColor){ Phone.prototype.changeColor(newColor); console.log("I, as a smart phone, am proud of my " + this.screenSize +" inch screen"); }; palmPhone = new SmartPhone('white', 4); palmPhone.changeColor('red');
Larry also mentioned the beauty of bind. I understand the concept but should have used them more frequently in all the jQuery callbacks.
var obj ={ cat: 'mii', cow: 'muu' }; var shout = function(input){ console.log(this[input]); }; shout('cat'); //undefined shout = shout.bind(obj); shout('cat'); //now always run in the context of 'obj'
Oh, for our entire class with 20+ people, we have 7 client projects to pick from. Everyone is supposed to submit their top 3 choices and we will know the result tomorrow. Since only 7 people will be picked, I guess I should be thinking hard about my Plan B. I am thinking about doing a Flickr client…