Define a function with a prototype chain

前端 未结 1 2105
温柔的废话
温柔的废话 2020-12-02 02:32

Suppose I have an object X defined as

var X = function () {};
X.prototype.doSomething = function () {};
X.prototype.doSomethingElse = function (         


        
相关标签:
1条回答
  • 2020-12-02 03:21

    No, it is not possible (in a standard way). Every possibility to create a callable object (i.e., a function) will create one inheriting from Function.prototype1; and you can't change the [[prototype]] of an object afterwards2.

    See also:

    • create function in javascript with custom prototype
    • Can you create functions with custom prototypes in JavaScript?
    • Is it possible to create a function with another prototype than Function.prototype?
    • Can a JavaScript object have a prototype chain, but also be a function?
    • Custom prototype chain for a function
    • Correct prototype chain for Function (for some internals)

    1: OK, ES6 allows us to subclass Function. But that's not as useful as it sounds.
    2: Since ES6, you can use Object.setPrototypeOf.

    0 讨论(0)
提交回复
热议问题