Hi folks,
I admit, I never liked javascript, so I avoided it as much as possible. For a couple of reasons - but here is not the place to discuss tastes. Rather, I am just looking for a way to understand what is actually going on in those code snippets.
Somehow accidentally I figured a little piece of code, which does actually work and do what I intended (and appears to be quite helpful), although I don't fully understand why (this is an example, there is a couple more of them):
Then, when adding an obtained package that would need the same startup, I noticed that
TypeError: _this.dataset is undefined
So I started to search for a way to get rid of
At the first
That bug of a language seems to work 100% by-reference, and there is no way around (at least I found none, and I tried all the usual tricks). Probably for 'security' reasons.
But then, the total surprize, writing it this way, it suddenly works again (and no 'security' anymore):
So what is going on here??
I admit, I never liked javascript, so I avoided it as much as possible. For a couple of reasons - but here is not the place to discuss tastes. Rather, I am just looking for a way to understand what is actually going on in those code snippets.
Somehow accidentally I figured a little piece of code, which does actually work and do what I intended (and appears to be quite helpful), although I don't fully understand why (this is an example, there is a couple more of them):
Code:
var startupFunc242 = function() {
var list; var i;
list = document.querySelectorAll('[data-href242]');
for (i = 0; i < list.length; i++) {
list[i].onclick = function() {
window.location.href=this.dataset.href242;
};
};
};
window.onload = startupFunc242;
Then, when adding an obtained package that would need the same startup, I noticed that
onload
does not stack. So, for concistency, I started to change all to addEventListener
. And that did not work:
Code:
list = document.querySelectorAll('[data-href242]');
for (i = 0; i < list.length; i++) {
list[i].addEventListener('click', () => {
window.location.href=this.dataset.href242;
});
};
TypeError: _this.dataset is undefined
So I started to search for a way to get rid of
this
- I don't like it anyway because I never know what this is actually supposed to be. And the fun increased: after a lot of unsuccessful tries I came across that one (which doesn't work either):
Code:
list = document.querySelectorAll('[data-href242]');
for (i = 0; i < list.length; i++) {
var s = list[i].dataset.href242;
alert(s);
list[i].addEventListener('click', function() {
alert(s);
window.location.href=s;
});
};
At the first
alert
, the proper string is present in the variable, but at the second it contains something like [object HTMLInputElement]. So it is not that the variable wouldn't be accessible, it is the content that isn't visible! That bug of a language seems to work 100% by-reference, and there is no way around (at least I found none, and I tried all the usual tricks). Probably for 'security' reasons.
But then, the total surprize, writing it this way, it suddenly works again (and no 'security' anymore):
Code:
document.querySelectorAll('[data-href242]').forEach((i) => {
i.addEventListener('click', () => {
window.location.href=i.dataset.href242;
});
});
So what is going on here??