Javascript function with prototype instead of class

Burperino

So I've been trying to make a javascript framework for fun. It's been going well so far, but I have one problem: functions with functions added in the prototype.

Code:

const Element = (() => {
    function _element(type, comp = [], attr = []) {
        const el = document.createElement(type);
        comp.forEach(p => el.appendChild(p));
        attr.forEach(a => el.setAttribute(a[0], a[1] === undefined ? a[1] : ""));
        
        return el;
    }

    _element.prototype.event = (name, callback) => {
        this.addEventListener(name, callback);

        return this; //Not sure what to do to make function chaining possible
    }

    return _element;
})();

I can use this "class" by using: const element = new Element(). But when I try to access the function "event" it is undefined. But it does show up with the visual studio code intellisense.

Thank you for your time.

b.stevens.photo

As @Jared Smith pointed out you're overriding the return value of your constructor by returning an html element. Arrow functions also don't give you access to the this context so chaining wouldn't have worked.

const Element = (() => {
    function _element(type, comp = [], attr = []) {
        this.el = document.createElement(type);
        comp.forEach(p => this.el.appendChild(p));
        attr.forEach(a => this.el.setAttribute(a[0], a[1] === undefined ? a[1] : ""));
        
    }

    _element.prototype.event = function(name, callback) {
        this.el.addEventListener(name, callback);
        return this;
    }

    return _element;
})();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaScript - Access Class Instance Property in Prototype Function

JavaScript: why use prototype to call a function instead of just calling the function?

Why use "[*]" instead of "[]" in function prototype?

prototype returning [function] instead of Array

Add a function to String class using JavaScript's String.prototype

JavaScript Prototype function Issue

Javascript composition prototype function

Accessing a Javascript prototype function

Prototype function JavaScript

javascript, add function to prototype

JavaScript function prototype alternatives

JavaScript: Assigning "this" to a prototype function

JavaScript: Function.prototype

JavaScript prototype delegation in function

Function with array and prototype javascript

The prototype (parent class) of a prototype object in Javascript?

Javascript Sub function in a Prototype function

Prototype a function to another function JavaScript?

Mock class prototype in javascript with jest

Copy Javascript "Class" Prototype to an Object

Why prototype consider in object but not in function instead

The prototype is returning my function instead of the return value

TypeScript Class how to define not prototype class function

Javascript func.prototype.apply() with a string instead of `this`

Is there a Javascript offchange="" function, or a way to replicate it by toggling using a class instead of value?

Javascript prototype function returning NAN

Javascript Get Prototype Function Name

What is the prototype of a Function object in Javascript?

Javascript Function.prototype.call()