1

Entry level. ES5 standard.

I have built a module:

import Person from './Person.js';

function Employer() 
{    
    Person.call(this);

    this.randomMethod = function() {
        console.log('randomMethod');
    }

    this.render = function(elem) {
        elem.innerHTML = this.compileTemplate();
    }

    this.compileTemplate = function() {
        return "<p onclick='this.randomMethod()'>Employer block</p>";
    }
}

export default Employer;

As you can see, it has its own view. So, now I am able to create as many objects as I want and bind them to DOM elements. As a next step I would like to teach them to execute its own methods (randomMethod in this case) without any global variables. To have a component, which knows how to render itself and how to process its events on es5 without any frameworks.

At least I need to emit a CustomEvent and I wouldn't like to have such long in-line code but a plain method call.

Something similar to Vue components, which have template and methods properties and you can call those methods from template.

Any ideas in this direction?

5
  • have you seen Custom Elements ? It works everywhere reasonable these days. Also, you don't need to call this.randomMethod, just randomMethod works because inline event handlers are invoked as though you called them as let event=arguments[0]; with(this){eval(attrCode)}. you can also shorten this.render = function(elem){ to render (elem) { if you put it in a class instead of a function. Commented Dec 21, 2022 at 23:36
  • Uncaught ReferenceError: randomMethod is not defined Commented Dec 21, 2022 at 23:54
  • in that case this.randomMethod() works as expected? Commented Dec 22, 2022 at 0:02
  • the same error. the only way to call the component's method is to get a pointer via a global variable (window object) and call it. Commented Dec 22, 2022 at 0:39
  • in my case pointer this within onclick event points on <p> html object Commented Dec 22, 2022 at 0:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.