What does the javascript “self” word limit to?
What does the javascript “self” word limit to?
I try to create a google maps marker with
var marker = new google.maps.Marker(
map: self.map,
position: new google.maps.LatLng(lat, lng)
);
which works fine, but if i try it at another position in my code, it doesn't create a marker with self.map.
The map is initialized with
self.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
i guess it is because of the self.
I have no clue what are the boundaries of self and why it works at one position of my code, but not at another position.
When i try to initialize the map with:
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
it works fine with creating a marker with that code:
var marker = new google.maps.Marker(
map: map,
position: new google.maps.LatLng(lat, lng)
);
at ANY position in my code.
Are there any boundaries with self. that i can avoid?
It is a template, that's why i have that problem.
Update:
If i try to add a marker with map in my code it says "Reference error: map is not defined".
With self.map it doesnt throw any error in my code, but the marker also doesnt get added.
It works with self.map for the code template, but not in my code.
The code is about 2000 lines, i don't know what part to post that could eventually help.
Could you possibly provide a working example of the issue? I think some additional context may be needed to answer. – In the language,
self
isn't a keyword and would only exist as a variable. So, it depends on where it's declared compared to these statements. Though, browsers do define a self
global variable, so it's possible to use that.– Jonathan Lonowski
Sep 1 at 17:11
self
self
By default
self
refers to the global object (window
) in a browser. People often use the name self
as an alias for this
when dealing with scoping problems, so self
can easily not refer to the same thing in different places. It seems like your use of self
is just a sloppy attempt to store things in the global scope. Storing things in the global scope is discouraged, but if you have to do so, why not just use window
?– JLRishe
Sep 1 at 17:12
self
window
self
this
self
self
window
If i try to add a marker with map it says "Reference error: map is not defined". It works with self: map for the code template, but not in my code. With self.map it doesnt throw any error, but the marker also doesnt get added. The code is about 2000 lines, i don't know what part to post that could eventually help.
– Roman
Sep 1 at 18:40
@Roy G no it isnt, please check thoroughly first.
– Roman
Sep 1 at 20:57
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
self refers to this, atleast that's what everyone will do, so can you add the initialization part of self and then show a minimum example to reproduce the actual error
– karthick
Sep 1 at 17:10