2
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.

1
  • Formal parameters to functions are very similar to variables. It's just a name that references whatever argument was passed to the function. Nearly the same as if you did var hash = arguments[0];, where arguments is a collection representing all the arguments passed when the function was invoked. Commented Aug 30, 2011 at 21:06

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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:

  1. 'hash' is an object that has a 'w' property.
  2. The value of the w property is an object with a method .fadeIn().
  3. Since fadeIn() is a relatively well known jQuery method, hash.w is probably a jQuery object.

Comments

2

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() { ... }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.