let obj = {
init: function (){
document.querySelector('#btn').addEventListener('click', this);
document.querySelector('#btn').addEventListener('focus', this);
document.querySelector('#btn').addEventListener('blur', this);
},
handleEvent: function(ev){
switch(ev.type){
case 'click':
this.something(ev);
break;
case 'focus':
this.something(ev);
break;
case 'blur':
this.something(ev);
break;
case 'explode':
break;
}
},
something: function (ev){
//gets called by click event list
console.log('btn was', ev.type, '-ed.');
}
}
//get things started
obj.init();
if we summarize the above code we get:
let obj = {
init: function (){},
handleEvent: function(ev){},
something: function (ev){}
}
//get things started
obj.init();
For me, this is a new concept. is it OOP JS or some other precise thing in JS. If I search it by what search term can I get some material to browse, and learn the missing concepts.
I tried browsing some videos on methods and OOPs, but this was specifically nowhere mentioned.
If I compare it to PHP it looks like some construction methods in JS.
Please advise me or if you can help me with correct search term or suggested reading that I should follow to get an understanding of what is happening here.
It’s a kind of magic. Practically, OOP is most universal software development concept. In some specific areas we can use something else (e.g. Functional Programming for mathematic), but for web nothing compares with OOP. And I see no any reason to avoid OOP in JS.
is not configurable at all, it just does one very specific thing - in OOP in general we have more general classes that can be composed together to perform some task - if #btn was a constructor argument for example it would become a lit but more useful as you could apply the same code in different situations, for different buttons.
the methods are not sharing any information of the class itself, which is another indication the class is just a “bag of functions”
So it being a class here adds absolutely nothing, functionally.
It seems to me rather than being a piece of code to demonstrate a particular pattern, the key purpose of the script is to illustrate handleEvent being a default callback for eventListeners.
As a result if we have a handleEvent method on our object, we only need to provide the context ‘this’ as a second argument to the eventListener rather than a named function/method.
Essentially as others have stated it is just an object literal — a container for methods and properties.
As an illustration, without init
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Document</title>
</head>
<body>
<button id='btn'>Button</button>
<script>
let obj = {
handleEvent: function(ev){
switch(ev.type){
case 'click':
this.something(ev);
break;
case 'focus':
this.something(ev);
break;
case 'blur':
this.something(ev);
break;
case 'explode':
break;
}
},
something: function (ev){
//gets called by click event list
console.log('btn was', ev.type, '-ed.');
}
}
document.querySelector('#btn').addEventListener('click', obj);
document.querySelector('#btn').addEventListener('focus', obj);
document.querySelector('#btn').addEventListener('blur', obj);
</script>
</body>
</html>