I'm a front-end developer with an interest in client-side development through the use of HTML, CSS & JavaScript. I'm an evangelist of HTML5, CSS3, jQuery, standardization & open-source. I don't design the sites you use, I make them work.
After a couple days into my reading on JavaScript's Prototype object, I'm becoming a bit more familiar with it. Still have some questions I need answered, but I'm guessing they'll be answered through experimentation and reading.
In the meantime, I figured I'd share how you assign custom methods and properties to an object, using prototype. This will allow all objects of that type to share them. First we'll start with the following object constructor. The object constructor is nothing more than a function, the only difference is how you call it, using the new keyword, which you'll see in a moment. The constructor will help define a particular object type, so you don't have to redefine a new instance of the same type, over and over.
I'm working my way through JavaScript: The Good Parts, and as I come across things I'm not familiar with myself, I'm sharing it. My hope is that some of my findings perk the interest of anyone who reads it and gets them to dive a bit deeper into JavaScript as well.
While reading about objects, Crockford describes the process of retrieving object values and assigning them to variable. While he didn't specifically outline that this is a performance benefit, I'm pretty sure it is, since you wouldn't have to "lookup" the value each time and could simply refer to the variable which holds that objects value.
Not a whole lot to really say on the topic yet, since I'm still getting into the Object-Oriented side of JavaScript, but I'm loving my newfound knowledge. In the code snippet below, I simply create a function to define the properties of the Person object and extend it using prototype, adding the setAge method.
If you have any comments on a more efficient way to set objects or extend them, I'm all ears. As I mentioned, I just started venturing into O-O JavaScript the last couple of days. Would love to hear any feedback you might have.
Yesterday, while looking through some of my very first JavaScript programs I came across something that made me chuckle a little bit. Then today, I was reading a JavaScript article online and saw the same concept I used early on, still being used in what was a relatively recent article.
As I submerse myself deeper and deeper into the world of JavaScript (although still not very deep), I continue to find intriguing ways of doing things. Lately, I've been wanting to test the performance of some various techniques, as well as native JavaScript versus jQuery.
There are numerous browser add-ons/plug-ins but that only really tells the story for that particular browser, and let's face it, not all browser JavaScript engines were created equal. But in doing some Google searches, I found an interesting console method. Unfortunately, I can't remember which site I saw it on, so I can't give proper credit.
Anyways, the method looked like this:
console.time('foo');// your JS code here
console.timeEnd('foo');