3 ms·
In JS, everything that's not a primitive is an object, and primitives can themselves be implicitly coerced to objects. That is to say, these are equivalent:
by crooked-v 8y ago
In JS, everything that's not a primitive is an object, and primitives can themselves be implicitly coerced to objects. That is to say, these are equivalent:
(new String("abc")).length
"abc".length
Functions are really just objects that can be invoked as a function, and they can have properties and even prototypes set the same as any other object. This used heavily in React:
function DivWrapper({ children }) {
return <div>{children}</div>
}
// show a nice name when debugging
DivWrapper.displayName = 'DivWrapper';
// verify incoming props when debugging
DivWrapper.props = {
children: PropTypes.node
};
- kevin_thibedeau 8y agoThe problem is that in the latter case the function object doesn't know what it's name is. This pattern gets unnecessarily abused just to be cool or follow the cargo cult.
- crooked-v 8y agoReact adopted displayName to avoid "the function object doesn't know its name" issues, as well as names being mangled by minifiers (or, in the future, stuff like WASM).