I have stumbled upon this code
var Actors = {
init: function(config){
this.config = config;
this.bindEvents();
},
bindEvents: function(){
this.config.letterSelection.on('change',this.fetchActors );
},
fetchActors: function(){
console.log('fetching');
}
};
And at first I was thinking what kind of Javascript is this. I don’t know what to search for. so I guess asking it here is approrpriate
What I understand is that var Actors is an object.
but what about these?
init: function(config){
this.config = config;
this.bindEvents();
},
What do you specifically call the literal “init” ? is it called an object literal? after the init it has a function attached to it what do you call it?
I understand what the code is doing. but I don’t know what to call them in technical terms.
so what type of javascript is this? why does a word “init” has a function to it
JavaScript is kinda functional language, which means that functions are first-class objects in it. So if you have a variable, then you can assign to it some string, some number, some object or some function, like this:
In the same way you can assign a function to an object property, like this:
But when you call your function like
a.myProp(), then this function also receives a special argument calledthiswhich points toa.So, in your case
Actorsis assigned object literal, which has a propertyinit, which is assigned a function. You can call itmethod, but this is a concept from other OO languages. Here, it’s just a property which is assigned a function.