pass hash as function parameter

G

giloosh

can i pass a hash as a function parameter. ive seen it been used
before but i can't figure out how to do it.

i would like to call a function like this for example

doSomething({variable1:"blabla",variable2:"blabla"});

how would function doSomething be written for this to work.

I would also like the hash to be optional and the variables in the
hash to be optional.
meaning i should also be able to call doSomething(); with no variables
in it.
 
D

David Golightly

can i pass a hash as a function parameter. ive seen it been used
before but i can't figure out how to do it.

i would like to call a function like this for example

doSomething({variable1:"blabla",variable2:"blabla"});

how would function doSomething be written for this to work.

I would also like the hash to be optional and the variables in the
hash to be optional.
meaning i should also be able to call doSomething(); with no variables
in it.

If by "hash" you mean "object literal" and not "a reproducible method
of turning some kind of data into a (relatively) small number that may
serve as a digital "fingerprint" of the data", then your function just
treats its first argument as an object. What's your question,
exactly? Could you provide more parameters as to what you're trying
to accomplish here?
 
L

Lee

giloosh said:
can i pass a hash as a function parameter. ive seen it been used
before but i can't figure out how to do it.

i would like to call a function like this for example

doSomething({variable1:"blabla",variable2:"blabla"});

how would function doSomething be written for this to work.

I would also like the hash to be optional and the variables in the
hash to be optional.
meaning i should also be able to call doSomething(); with no variables
in it.

Judging by the fact that you have an example of the calling
syntax but don't know what to do with it, and that you also
don't know how to use the SHIFT key, I'm betting that you're
a particularly slow student and that this is a class assignment.


--
 
G

giloosh

ok forget the word "hash" i dont know why i called it that. for some
reason i thought you call something like this a hash:
{name:"mike",age:"12",gender:"male"}

it's stores data like an array, with key names and values. name would
be the key name and "mike" would be the value.
what do you call it? JSON? i just thought JSON was a pretty new term,
and there was a name for it before JSON.

what ever its called... how would i pass something like that as a
function parameter.
how would the function have to be written?
 
R

RobG

ok forget the word "hash" i dont know why i called it that. for some

Please don't top-post, reply below trimmed quotes.
reason i thought you call something like this a hash:
{name:"mike",age:"12",gender:"male"}

Some do, but the preferred term is object literal because that is what
it is. Terms like hash or associative array imply more than what a
javascript Object delivers, so while a javascript Object looks
something like what might be called a hash in other languages, it
lacks many of the features that would make it a hash.

it's stores data like an array, with key names and values. name would

In javascript, Array's are objects too. :)
be the key name and "mike" would be the value.
what do you call it? JSON? i just thought JSON was a pretty new term,
and there was a name for it before JSON.

JSON is otherwise called object literal notation.

what ever its called... how would i pass something like that as a
function parameter.
how would the function have to be written?

Iterate over the arguments list, check whether each is an Object or
Function, then iterate over their properties. Something like:


<div id="xx"></div>
<script type="text/javascript">

function foo()
{
var i, len, obj, p, msg = [];

for (i=0, len=arguments.length; i<len; i++){
obj = arguments;
msg.push('Arg ' + i);

if (typeof obj == 'object' || typeof obj == 'function') {

for (p in obj) {
msg.push(p + ': ' + obj[p]);
}
}
msg.push('');
}
document.getElementById('xx').innerHTML = msg.join('<br>');
}

window.onload = function()
{
foo(
{ foo:'bar', ipsum:'lorem'},
{ bit:'bun', colum:'semper'}
);
}
</script>


You may want to test if obj.hasOwnProperty(p) or propertyIsEnumerable
are true to avoid properties in the prototype chain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top