简体   繁体   English

括号的功能是什么意思?

[英]What is function with brackets mean?

Does anyone know what is test[name] mean? 有谁知道什么是test[name]是什么意思?

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. 这会将一个名为“foo”的属性添加到对象test ,该属性的值是一个函数。 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. 在这种情况下,对象test实际上是一个函数并不重要,您可以像在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' . 请注意,如果将name变量分配给不同的name ,则test[name]()将不起作用,例如name = 'bar'

Javascript has two sets of notation for accessing objects, dot notation (obj.property) and bracket notation (object[property]). Javascript有两组用于访问对象的表示法,点表示法(obj.property)和括号表示法(object [property])。 More on that at MDN . 有关MDN的更多信息。

test[name] = function (){} assigns an anonymous function to the name property on the the test object (which itself is a function). test[name] = function (){}test对象上的name属性指定一个匿名函数(它本身就是一个函数)。 In this case (as noted by the comments) the variable name is being used to access the property. 在这种情况下(如注释所述),变量name用于访问属性。

This may seem a little strange at first, but it's helpful to remember that in javascript, functions are objects. 起初这看起来有点奇怪,但记住在javascript中,函数是对象是有帮助的。

All functions in Javascript are also objects. Javascript中的所有函数也是对象。 This adds a property to the test function object with a value which is an anonymous function. 这会向test函数对象添加一个属性,其值为匿名函数。

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 . 在JavaScript中, 函数是对象 They have properties. 他们有财产。 test[name] sets a property (named whatever the name variable holds) to a function. test[name]为函数设置一个属性(命名为变量name )。

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: 当你有一个具有已定义属性的javascript对象时,你可以使用点符号obj.property或方括号符号obj[property]访问属性,该属性也可以是一个函数,所以如果你有一个对象:

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. 你可以通过test['foo']('bar')调用test.foo('bar')这在迭代中或者当你不知道属性的名称将会是什么时特别有用。 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 取自Mozilla页面

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) ) 意思是:有(如果一切正常)两个对象: testname (我们知道至少test存在,因为你之前定义了一行: function test(value)

take the test object (if there isn't a test object an error will happen). 获取test对象(如果没有测试object ,则会发生错误)。 Then access the key/value pair with the key calculated from the name object and there put a function. 然后使用从name对象计算的密钥访问键/值对,并放置一个函数。

Now, how the key is calculated from the name object? 现在,如何从name对象计算密钥? 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. 任何非字符串对象(包括数字)都通过toString方法进行类型转换为字符串。

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" ) 请注意,描述有点不对... test[null] == test["null"]test[undefined] == test["undefined"] ,所以也许真相就是像String(key).valueOf()一样的东西String(key).valueOf()完成( String函数将null转换为"null"undefined"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对象的哈希值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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