最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

does (hash) mean anything as a function parameter in javascript? - Stack Overflow

programmeradmin0浏览0评论
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.

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.

Share Improve this question asked Aug 30, 2011 at 21:01 DamonDamon 10.8k17 gold badges91 silver badges146 bronze badges 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. – user113716 Commented Aug 30, 2011 at 21:06
Add a ment  | 

3 Answers 3

Reset to default 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.

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.

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 mon 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() { ... }
}
发布评论

评论列表(0)

  1. 暂无评论