Home Javascript: How to correctly create a new custom istance from object definition (like a class)
Reply: 0

Javascript: How to correctly create a new custom istance from object definition (like a class)

user1415
1#
user1415 Published in April 26, 2018, 7:44 am

I see that is still not possible to use class freely thanks to IE11.

So I would like to use prototypes and object.create in order to create my istances in the most lightwight way possible. However I only found to go with Object.assign() which is a sort of merger of object not a inizializator.

var classDefiner = {
   x: 1,
   y: 2,
   standardFunction : function() {
       return this.x+this.y;
   }
};

var istance = Object.assign({}, classDefiner ,{
    x: 10,
    z: 3,
    customFunctionA : function() {
        return this.x+this.y+this.z;
    },
    customFunctionB : function() {
        return this.x/this.y;
    }
});

Honestly this sound to me just a redundant copy of all methods of classDefiner in {}. Not a great use of prototypes or class, or object.create(). Is this the right way or is there something I can improve?

PS: The Challenge is to do not declare any single function/method but use instead a {} wrapper for the class and another for the istance for the sake of readability. I know I can add many method manually by using istance.newMethod or even newobject.prototype.newMethod. But this is long and messy if you have tons of method to add.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306722 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO