Reputation: 10809
var newFieldShow = function(hash) {
hash.w.fadeIn("2000");
};
I see this in some code I have retrieved from online. I've never seen hash mentioned.. I am trying to determine if it has something to do with hashing, or if it's just an event reference like function(event)
which I'm used to seeing, and curious as to why it is being used here.
Upvotes: 2
Views: 813
Reputation: 707318
In this context, hash
is just the name given to a function parameter and has no special meaning beyond "the internal name (within the function) of the first parameter passed to the function named newFieldShow
". The name "hash" is not a reserved name in Javascript.
In general programming, the term hash
is often short for an object or thing with "hash-table like" capabilities. A hash table provides fast lookup of a piece of data when given a key. Javascript has similar types of capabilities in it's object type.
obj["foo"] = "One fine day";
console.log(obj["foo"]); // outputs 'One fine day'.
In the specific case you asked about, all we can see from the couple lines of code you have included is that:
w
property is an object with a method .fadeIn()
. fadeIn()
is a relatively well known jQuery method, hash.w
is probably a jQuery object.Upvotes: 5
Reputation: 39620
The name of a variable is just a hint at what the function expects as input, but there is no real "type hinting" in Javascript that would enforce any such policy.
Hash/object are used interchangeably in Javascript because object members can also be accessed in a fashion that is similar to the syntax of how you would access entries in a hash table in other languages.
hash.w
is equivalent to
hash["w"]
The latter is a syntax common in languages such as Python or Ruby (in fact the class implementing this behavior is called "Hash" in Ruby).
So the word "hash" does not refer to a cryptographic hash or a hash function but rather to the functionality of a hash table.
Objects are often referred to as "Hashes" in Javascript if they are merely a collection of key/value pairs but don't implement any functions, e.g.
hash = {
a: 5,
b: "string",
c: 7
}
opposed to
object = {
member_a: 5,
member_b: "string",
do_this: function() { ... },
do_that: function() { ... }
}
Upvotes: 2
Reputation: 7326
If someone sent me this code I would say that hash is an object with a property named w that looks to be a jQuery object (because of the fadeIn method).
hash could mean anything. Thus, the need to properly name variables (and function parameters) that make sense.
Upvotes: 5