gpt4 book ai didi

javascript - 以下脚本中的 'foo'和 'bar'是什么,需要定义吗?

转载 作者:行者123 更新时间:2023-11-29 20:42:52 24 4
gpt4 key购买 nike

只是一个示例代码:

  function process(elements) {
var total = 0;
for (i = 0; i < elements.length; i++) {
if (elements[i].value) {
total++;
}
}
if (total) {
return {
foo: 'Wow!'
};
} else {
return {
foo: 'Great!',
bar: 'Bravo!'
};
}
}

foobar 是否称为属性?它们是否需要声明,例如:

var foo;
var bar;

最佳答案

像这样的线条

  return {
foo: 'Great!',
bar: 'Bravo!'
};

正在返回 object这是使用 initializer notation 即时初始化的.

Objects can be initialized using new Object(), Object.create(), or using the literal notation (initializer notation). An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

所以 foobar 是属性名,因此不需要用 var 声明。

The terms foobar (/ˈfuːbɑːr/), or foo and others are used as placeholder names (also referred to as metasyntactic variables) in computer programming or computer-related documentation.[1] They have been used to name entities such as variables, functions, and commands whose exact identity is unimportant and serve only to demonstrate a concept.

您可以在 wikipedia 上阅读更多关于历史和词源的信息.

关于javascript - 以下脚本中的 'foo'和 'bar'是什么,需要定义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com