Skip to content

Conversation

LinusU
Copy link

@LinusU LinusU commented Mar 14, 2020

Fixes #388

@Qix-
Copy link
Member

Qix- commented Mar 15, 2020

I'm not at a machine right now that I can test this; does this apply to e.g. chalk.red or only the root chalk object?

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Apr 2, 2020

A better option might be to do:

Object.setPrototypeOf(Chalk.prototype, Function.prototype);

@LinusU
Copy link
Author

LinusU commented Apr 3, 2020

@ExE-Boss Hmm, actually, Chalk is already a function, so it should already have the function prototype, but then why isn't it working as it is? 🤔

function Chalk () { ... }

const chalk = {}
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_)

Object.setPrototypeOf(chalk, Chalk.prototype)
Object.setPrototypeOf(chalk.template, chalk)

Shouldn't the prototype chain here be chalk.template -> chalk -> Chalk.prototype -> Function.prototype? Or am I missing something? 🤔

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Apr 3, 2020

The prototype chain is: chalk.template → chalk → Chalk.prototype → Object.prototype → null.

@LinusU
Copy link
Author

LinusU commented Apr 3, 2020

Screenshot 2020-04-03 at 14 01 02

Hmm, I just don't understand why Chalk.prototype is pointing to Object.prototype when it's just defined as a function in the code. I mean obviously I'm missing something 😁

@ExE-Boss
Copy link
Contributor

ExE-Boss commented Apr 3, 2020

That’s standard ECMAScript behaviour: https://tc39.es/ecma262/#sec-makeconstructor.

The prototype property of constructable functions is initialised to a plain object with its prototype set to Object.prototype.

This object is used to create new instances by the new operator and to check for inheritance by the instanceof operator.

@LinusU
Copy link
Author

LinusU commented Apr 3, 2020

Ahh, of course 🤦‍♂

Thanks for pointing me in the right direction!

Yeah, doing Object.setPrototypeOf(Chalk.prototype, Function.prototype) is probably a better option, will update the PR 👍

@chalk chalk deleted a comment Nov 5, 2020
@Qix-
Copy link
Member

Qix- commented Nov 20, 2020

Ping @LinusU

Base automatically changed from master to main January 23, 2021 18:54
@sindresorhus
Copy link
Member

Closing for lack of response.

@LinusU
Copy link
Author

LinusU commented Apr 16, 2021

Sorry for the lack of response. I don't personally need this anymore since I don't use apply now that ... is natively supported.

@LinusU LinusU deleted the function-proto branch April 16, 2021 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing standard Function prototype (e.g. chalk.apply is not a function)
4 participants