4
I am "joking" with Javascript and came across a question. I understand that when my HTML page is loaded, if I want to already execute a function without having to declare it, I just have to define this direct function, like this:
(function(){console.log('foo');})();
My doubt (because I couldn’t make it work) is how I can perform a dual function in the same statement, something like this (in my view):
(
function(){
var b = 'hello world!';
console.log('bar');
},
function(b){
console.log('foo ' + 'b'); //o "b" declarado anteriormente na 1ª função
}
)();
Is there such a thing? I basically want such a function to perform after the first but, that the second consumes the same effort of the DOM...
Man, fantastic this... I’m studying these IIFE, detail that just had seen this in some codes on the internet, not even name I knew... Now I’m gonna dig in hehe, thank you!
– LeandroLuk
@Leandroluk, as I said, I see little use in IIFE, I prefer to define a "Class", if I need some property calculated or read-only, use Object.defineProperty, and to define the methods use "Class". prototype.
– Tobias Mesquita
Yes in certain aspects fully agree, my problem is that in my case I am constituting a plugin for Chrome that will handle a site already created, and I am testing methods of not declaring elements just reconfigure the site as I want, rsrs
– LeandroLuk
You can create a handleEvent as the second example of this LINK. In this case handleEvent will function as a closure for your plug-in (isolating it) and allow you to organize your code.
– Tobias Mesquita