I'm just learning about JS and oop and was wondering whether creating objects in HTML and calling fuctions there is considered a bad practice. For example in the onclick event like in the example bellow. Also is it allowed to have functions which are not methods? Like having one function where I'd be creating all the objects and calling their methods.
<input id="first_name" type="text" placeholder="First Name">
<input id="second_name" type="text" placeholder="Second Name">
<button onclick="const name = new Person('first_name', 'second_name', 'output'); name.writeName()">Show name</button>
<p id="output"></p>
class Person {
constructor(first_name_id, second_name_id, output_id) {
this.first_name = document.getElementById(first_name_id)
this.second_name = document.getElementById(second_name_id)
this.output = document.getElementById(output_id)
}
writeName() {
return this.output.innerHTML = "Your name is" + this.first_name.value + " " + this.second_name.value
}
}
<a onclick=...) but using JavaScript in most other forms isn't (evalandwithbeing rare exceptions).