简体   繁体   中英

What is function with brackets mean?

Does anyone know what is test[name] mean?

function test(value){
  copy(value||{},this);
}
test[name] = function(){
    return "test"
}

This will be easiest to explain with an example:

var name = "foo";
test[name] = function(){
    return "test"
};

This would add a property named "foo" to the object test , and the value of that property is a function. It doesn't matter in this case that the object test is actually a function, you can assign properties to functions just like any other object in JavaScript.

You could call this function using any of the following methods:

  • test[name]()
  • test["foo"]()
  • test.foo()

Note that test[name]() will not work if the name variable is assigned to something different, for example name = 'bar' .

Javascript has two sets of notation for accessing objects, dot notation (obj.property) and bracket notation (object[property]). More on that at MDN .

test[name] = function (){} assigns an anonymous function to the name property on the the test object (which itself is a function). In this case (as noted by the comments) the variable name is being used to access the property.

This may seem a little strange at first, but it's helpful to remember that in javascript, functions are objects.

All functions in Javascript are also objects. This adds a property to the test function object with a value which is an anonymous function.

For example:

function test(){
  return "foo";
}

// test is a function, so it is also an object and 
// it can have properties assigned to it
test.x = function(){
   return "bar";
};

test();   // "foo"
test.x(); // "bar"

Of course just like with any object you can also use bracket notation :

var name = 'hello';
test[name] = function(){
  return "HELLO!";
};

test.hello(); // "HELLO!"

In JavaScript, functions are objects . They have properties. test[name] sets a property (named whatever the name variable holds) to a function.

when you have a javascript object with defined properties you can access the property either with the dot notation obj.property or with the square brackets notation obj[property] the property could also be a function so if you have an object:

var test = { foo : function(arg){ console.log(arg) }, bar : 'hello' };

you can call test.foo('bar') also by doing test['foo']('bar') This is especially useful in iterations or when you dont know a priori what the name of the property is going to be. For example:

var fun = 'foo';
test[fun]('hello world');

Naturally it's up to you to do proper checks such as

if ('function'==typeof test['foo']){ test['foo']('bar'); }

Also note that you can do checks on the fly like so:

test[ fun || 'foo']('hello');

Taken from the Mozilla page

One can think of an object as an associative array (aka map, dictionary, hash, lookup table). The keys in this array are the names of object members

There are two ways to access object members: dot notation and bracket notation (aka subscript operator).

So

test[name] = function (

means: there are (if everything is ok) two objects: test and name (and we know that at least test is present, because you defined it one line before: function test(value) )

take the test object (if there isn't a test object an error will happen). Then access the key/value pair with the key calculated from the name object and there put a function.

Now, how the key is calculated from the name object? The same page from before tells us:

Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.

Note that the description is a little wrong... test[null] == test["null"] and test[undefined] == test["undefined"] , so perhaps the truth is that under the covers something like String(key).valueOf() is done (the String function will convert null to "null" and undefined to "undefined" )

Some examples (where => means "is equivalent to, with this values")

var name = 'foo';
test[name] => test['foo']

var name = 123;
test[name] => test['123']

var name = 123.3;
test[name] => test['123.3']

var name = new Date();
test[name] => test['Wed Aug 14 2013 17:35:35 GMT+0200 (...)']

var name = null;
test[name] => test['null']

var name = undefined;
test[name] => test['undefined']

var name = [];
test[name] => test['']

var name = [1,2,3];
test[name] => test['1,2,3']

var name = {};
test[name] => test['object Object']

and so on...

括号是如何通过键将属性引用到javascript对象的哈希值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM