javascript - Extending es6 classes programmatically - Stack Overflow

Using standard es5 I have this method that allows me to add methods to my library's prototype chai

Using standard es5 I have this method that allows me to add methods to my library's prototype chain (it allows extension of the core library and also any ponents that are attached to the library):

 library.extend = function(extendId, extendObj) {
        //if it's an extension of the core library object...
        if(extendId === 'library') {
            library.prototype[extendObj.name] = extendObj.func;
        } else {
            libraryponent[extendId].prototype[extendObj.name] = extendObj;
        }
 }; 

Usage would be:

 /* create some new class */
 var someponent = function() {}
 someponent.protoype.somemethod = function() {}

/* extend the base libraries prototype chain with the new class*/
library.extend('library', someponent)

In es6 classes we also have prototypes but they are masked by the class syntax and you are supposed to add methods to the class using the extends method.

Because of this I'm not sure how I can programmatically add methods to es6 classes using a method similar to what I have above.

Using standard es5 I have this method that allows me to add methods to my library's prototype chain (it allows extension of the core library and also any ponents that are attached to the library):

 library.extend = function(extendId, extendObj) {
        //if it's an extension of the core library object...
        if(extendId === 'library') {
            library.prototype[extendObj.name] = extendObj.func;
        } else {
            library.ponent[extendId].prototype[extendObj.name] = extendObj;
        }
 }; 

Usage would be:

 /* create some new class */
 var someponent = function() {}
 someponent.protoype.somemethod = function() {}

/* extend the base libraries prototype chain with the new class*/
library.extend('library', someponent)

In es6 classes we also have prototypes but they are masked by the class syntax and you are supposed to add methods to the class using the extends method.

Because of this I'm not sure how I can programmatically add methods to es6 classes using a method similar to what I have above.

Share Improve this question edited Nov 17, 2015 at 10:18 Mike Rifgin asked Nov 17, 2015 at 10:08 Mike RifginMike Rifgin 10.8k22 gold badges77 silver badges115 bronze badges 4
  • Without showing how you use this code, it's very hard to understand what you're asking. However... Generally ES2015 classes can be considered to be "normal" (ES5) functions. – Amit Commented Nov 17, 2015 at 10:13
  • 1 you should be able to extend prototype. class is more-less just syntactic sugar. – webduvet Commented Nov 17, 2015 at 10:14
  • @ Amit I've added a usage example, although I think the code speaks for itself really....it's just extending a prototype chain. – Mike Rifgin Commented Nov 17, 2015 at 10:19
  • @webduvet - so I should just basically continue with directly extending the prototype then... – Mike Rifgin Commented Nov 17, 2015 at 10:20
Add a ment  | 

2 Answers 2

Reset to default 3

I think you have some confusion.

In ES5, functions created with function expression or declaration are both instantiable (i.e. constructors) and callable:

function f() {}
f();     // Function call
new f(); // Constructor instantiation

Then ES6 allows to create objects which are only callable or only instantiable:

var f = () => {}; // Arrow function
f();              // Function call
new f();          // Error: f is not a constructor

class f {};       // Class
f();              // Error: Cannot call a class as a function
new f();          // Constructor instantiation

That is, ES6 classes are just objects with a [[Construct]] internal method and a prototype property. You can treat them exactly as ES5 constructors.

So the usage would be

class someponent { /* create some new class */
  somemethod() {}
}

/* extend the base libraries prototype chain with the new class*/
library.extend('library', someponent)

where library.extend is the current one.

In es6 classes we also have prototypes but they are masked by the class syntax and you are supposed to add methods to the class using the extends keyword.

I'm not sure what you mean by "masked". Yes, it's different syntax, but the oute is quite the same - class creates a constructor function with a .prototype property. So while the extends syntax is of course nicer to write, you cannot use it programmatically. Notice that extends is used for subclassing, not for extension of existing classes, so it doesn't fit your need anyway.

I'm not sure how I can programmatically add methods to ES6 classes using a method similar to what I have above.

Just continue to use exactly the method you already have. It's totally fine to do mixins in that style.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637246a4637469.html

相关推荐

  • javascript - Extending es6 classes programmatically - Stack Overflow

    Using standard es5 I have this method that allows me to add methods to my library's prototype chai

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信