On Jun 24, 4:07 pm, Nic Hubbard <nnhubb...@gmail.com> wrote:
> I have used an object in the past as a function argument, but this was
> for a plugin that I wrote. Using it in the architecture of a plugin
> it worked.
>
> BUT, this time, I just want to write a normal function, but still use
> an object to set defaults and pass in changes to those defaults
> through as a param. Is this possible, or do I have to make this a
> jQuery function like $.myfunction() ?
>
> I am trying:
>
> function test(defaults)
Including an identifier name in the formal parameter list is
equivalent to declaring that parameter in the body of the function.
> {
> var defaults = {
Here you declare the variable again - there is no need for that.
> test: ''
> };
and assign it a value. Whatever was passed to the function as the
value of defaults has now been replaced by this assignment.
>
> alert(defaults.test);
>
> }
>
> test({test: 'It works!'});
>
> The above does not seem to work,
Where "work" has some special meaning for you. It does exactly what
it is coded to do: the object you pass to the function is replaced by
the internal assignment before the alert is called, so you see the
internally assigned value, not the one you pass.
> and replace the default with that was
> passed in the function. What am I doing wrong here?
function test(defaults) {
if (typeof defaults == 'undefined') {
// no useful parameters were passed to the function,
// assign a default value to defaults
defaults = {test: 'default value'};
}
alert(defaults.test);
}
--
Rob