Does (function() {})(); affect Imported Variables?

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I noticed that when I do a (function() {})() and put all the elements inside the {} and get out of there to declare imported values, it says some of the variables are unreadable. What's the proper way of using Imported together with (function() {})()?
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
What do you mean with Imported Variables?
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
I did make import function outside the (function() {})(), namely:

var Imported = Imported || {};Imported.Milena= true;var firstplugin = Milena|| {};firstplugin .Milena = {};and added variables that makes its value to the params. Somehow it says its having an error when you put or assign them again side the function.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
Anything you declare outside the anonymous function can be access both inside and outside.

Anything you declare inside it, can only be used inside it.

Try running this and you'll understand:

Code:
var Imported = Imported || {}; (function(){  Imported.Something = true;   var localVariable = 15;})(); console.log(Imported);console.log(localVariable);
 

Necromus

Veteran
Veteran
Joined
Mar 14, 2012
Messages
490
Reaction score
61
First Language
German
Primarily Uses
Anything you declare outside the anonymous function can be access both inside and outside.

Anything you declare inside it, can only be used inside it.

Try running this and you'll understand:

var Imported = Imported || {}; (function(){  Imported.Something = true;   var localVariable = 15;})(); console.log(Imported);console.log(localVariable);
But isn't that completely independant from this 'anonymous' function?

Anything declared with 'var' inside a function is private and can't be read from the outside, that should have nothing to do with whatever

(function(){

})();

actually does right?
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
Yes.

This:

(function(){  var localVariable = 15;}();
is the same thing as this:

function doSomething() {  var localVariable = 15;} doSomething();
The localVariable won't exist outside the function.
 

Necromus

Veteran
Veteran
Joined
Mar 14, 2012
Messages
490
Reaction score
61
First Language
German
Primarily Uses
Yes.

This:

(function(){  var localVariable = 15;}();is the same thing as this:

function doSomething() {  var localVariable = 15;} doSomething();The localVariable won't exist outside the function.
So for better understanding of that construct...

Does that mean its a self...calling/executing function?

So the

(function(){

})();

part calls the function o n its own?

Or is it simply just some shortend/different way to type a function?
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
Yes, the bolded part is executing the function.

It's the same as doing this:

Code:
var x = (function(){  // do something}); x();
 

Galv

Veteran
Veteran
Joined
Oct 1, 2012
Messages
1,306
Reaction score
1,575
First Language
English
Primarily Uses
RMMZ
(function(){
})();

This is an immediately executed function. The () at the end is the code that executes the function you've created there at the time it was created :)
 
Last edited by a moderator:

Necromus

Veteran
Veteran
Joined
Mar 14, 2012
Messages
490
Reaction score
61
First Language
German
Primarily Uses
Well cool, so like I thought.

Just wondering cause I kept hearing 'anonymous' associated to that construct, but that has nothing to do with it after all, its just the way defined variables work in JS.

If you do write plugins like that, is there even a need to pass them along to the plugin manager then?

Still figuring out how MV flows, where it starts and so on.

I can read the code now, most of it anyways, but finding your starting point is still pretty hard :guffaw:
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Gosh, it seems there's so much to learn. :/
 

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
I personally create everything inside a module so that its all scoped, of course global RPG Maker Stuff isn't scoped to my module but thats ok. How ever all the classes, all the methods and all that jazz that I write is scoped.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,078
Members
137,580
Latest member
Snavi
Top