简体   繁体   中英

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

I found @@functional/placeholder in ramda source code.

Code Link in 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?

It's very weird. I never see this usage and i wonder what this is.

(Ramda author here)

Both Bergi's comment and Nicholas Tower's answer are correct. 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. 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. 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. 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. Or we could have just used undefined as the signal, but that would not allow someone to actually use undefined as a value.

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.

It's just a property on an object. It has no special meaning, except for the meaning ramda gives to it. 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).

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

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