Posts tagged objects

Object Constructors vs. Object Literals – Theory and Practice

0

I have noticed recently some discussions about the comparison/contrast between object literals and objects created by a constructor. In the spirit of getting some information out there for people looking to better their skills in JavaScript and jQuery, I’ll go through some of the benefits of each method. (more…)

Building Better Objects

0

Here is the first in the series on 5 Things Everyone Could Learn From the jQuery Source

1) Building Better Objects

One of the things which is most remarkable about the jQuery source is the efficiency. This code has been stripped down pretty much as far as it can be, making it leaner and meaner than most other frameworks out there. The technique I am going to show you here is a perfect example of that, as well as being a very good coding practice that all developers should be using. On line 317 of the jQuery 1.5 source, we have the following code:

target = arguments[0] || {},

So, what this says is: You have a variable named ‘target’, and into it we want to store some stuff. Because that’s what variables are for. Like your Dad’s garage, they are for storing stuff that you will (hopefully) use later. Into target, we want to store the first member of the arguments array. How do we know arguments is an array? Well, we don’t really. And even if arguments was an array, we don’t have a guarantee that we will have something useful stored in arguments[0]. (more…)

Go to Top