I’m splitting up a .js script file into little .js scripts for a modular approach and for conditional script loading. I have a single object that contains all my methods, hence I’m splitting up its functions to those script files as well.
Which is a better implementation to add functionality to the object? this way:
jQuery.extend(myObj, {
myNewFunc : function() {
...
}
});
or this way:
myObj.myNewFunc = function() {
...
};
I’m doing the first option now since my functions were already declared like that when they were all in a single file, but I just realized that you can do it the second way too.
The two ways are basically identical. The only difference is, as you say, the notation.
Under the hood,
extenddoes the same thing as you do when actually adding the functions to the target object. It just iterates over the object literal that was passed, so you can add “batches” at a time.