Is it possible to create a function with another prototype than Function.prototype?

前端 未结 4 540
一个人的身影
一个人的身影 2020-12-11 06:17

I am working on a parser combinator library in JavaScript. For that I want to create functions that can be called like any other functions, but also have member functions th

4条回答
  •  情歌与酒
    2020-12-11 06:56

    A short answer to your question would be: Yes.

    function Parent() {
        // constructor
    }
    
    function Child() {
        // constructor
    }
    
    Child.prototype = new Parent();
    

    Here's how to add methods to your Child class:

    Child.prototype.someMethod = function() {
        // do something
    }
    

    It's been a long time that I've used Javascript in an object-oriented fashion unfortunately, but I believe there are some solutions to make the syntax a little bit clearer.

提交回复
热议问题