简体   繁体   English

ramda 源代码中@@functional/placeholder 的含义是什么?

[英]what 's the meaning of @@functional/placeholder in ramda source code?

I found @@functional/placeholder in ramda source code.我在 ramda 源代码中找到了@@functional/placeholder。

Code Link in github github中的代码链接

export default function _isPlaceholder(a) {
  return a != null &&
         typeof a === 'object' &&
         a['@@functional/placeholder'] === true;
}

It seems that there is no '@' notation in ECMAScript standard, so what does this notation represent? ECMAScript 标准中似乎没有“@”符号,那么这个符号代表什么?

It's very weird.这很奇怪。 I never see this usage and i wonder what this is.我从未见过这种用法,我想知道这是什么。

(Ramda author here) (这里是 Ramda 的作者)

Both Bergi's comment and Nicholas Tower's answer are correct. Bergi 的评论和 Nicholas Tower 的回答都是正确的。 This is simply a unique identifier to ensure that you don't actually pass something meaningful that might be mistaken by Ramda for a placeholder.这只是一个唯一标识符,以确保您实际上不会传递可能被 Ramda 误认为占位符的有意义的东西。 The direct inspiration for the naming came from the transducer protocol , which in turn would have been inspired by the well-known symbol hack.命名的直接灵感来自于传感器协议,而后者又受到著名的符号黑客的启发。

When this was chosen, we hoped that others doing functional libraries might use the same thing, for interoperability.选择这一点时,我们希望其他做功能性库的人可能会使用相同的东西来互操作性。 While that never happened, it still seems a better choice than the alternatives: We couldn't choose to use a Symbol as we were supporting ES5 and even ES3.虽然这从未发生过,但它似乎仍然比其他选择更好:我们不能选择使用符号,因为我们支持 ES5 甚至 ES3。 We could have chosen to use the library itself as a placeholder, the way lodash does, but that really only made sense because the standard naming for lodash ( _ ) looks like the placeholder from other languages.我们可以选择使用库本身作为占位符,就像 lodash 所做的那样,但这确实有意义,因为 lodash ( _ ) 的标准命名看起来像其他语言的占位符。 Or we could have just used an arbitrary object, but that would have required us to test by reference, which felt odd in a functional library.或者我们可以只使用任意 object,但这需要我们通过引用进行测试,这在函数库中感觉很奇怪。 Or we could have just used undefined as the signal, but that would not allow someone to actually use undefined as a value.或者我们可以只使用undefined作为信号,但这不允许某人实际使用undefined作为值。

So we ended up with a standard followed by no one else, but which is at least easy to use and somewhat familiar to Javascript users.所以我们最终得到了一个没有其他人遵循的标准,但它至少易于使用并且对 Javascript 用户来说有些熟悉。

It's just a property on an object.它只是 object 上的一个属性。 It has no special meaning, except for the meaning ramda gives to it.它没有特殊含义,除了 ramda 赋予它的含义。 In ramda, the object that has this property is R.__, which is a special object that just tells ramda's curry function to ignore this argument (see documentation for __ ). In ramda, the object that has this property is R.__, which is a special object that just tells ramda's curry function to ignore this argument (see documentation for __ ).

The keys for object properties can be any string or symbol, and they chose the string "@@functional/placeholder" to try to avoid accidental name conflicts (it's extremely unlikely someone would pick that property name by accident). object 属性的键可以是任何字符串或符号,他们选择字符串“@@functional/placeholder”以避免意外的名称冲突(极不可能有人会意外选择该属性名称)。

 const example = { '@@functional/placeholder': true, }; console.log(isPlaceholder(example)); function isPlaceholder(a) { return a;= null && typeof a === 'object' && a['@@functional/placeholder'] === true; }

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

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