-13

I have a question which was closed. The only hint I got in comments was ambiguous in a way that the wrong interpretation appeared to be the only one, hence, making the suggestion non-effective.

The user also posted a reference to his suggestion. It was a rather complex read so it took me two days to research it and sort it out. Finally I solved it "my way" and made it work, only to realize that the original hint was perfectly correct! The problem is not what to add to my code. It's where or rather how.

I'm pretty sure that there are others that will make the same mistake because they're used to how Angular manages the code (which is intuitively this'ed like with labmda syntax for functions in JS). That's why I'd like to re-open my question so I can provide the suggestion of @CrazyMonkey in a way that would've helped me quicker. There's no need to rewrite the question as it was, apparently, asked correctly, leading to a helpful comment.

How do I do that? Do I do that?

4
  • 5
    Why would you need to answer one of the most frequently asked questions for JS? Yes, the suggestion was spot-on because we get a dozen of these questions a day.
    – VLAZ
    Commented Mar 13, 2022 at 9:02
  • @VLAZ Because it relates to Angular which makes the JS-obviousness far less obvious. The binding concept is not used in Angular in practical cases ever. I recall my React project where it was quite basic stuff to bind components to each other but in Angular, I've never had that need (since 2016 beta version). So I figure it's "different worlds". Commented Mar 13, 2022 at 10:23
  • 3
    "The binding concept is not used in Angular in practical cases ever." yes and there are other ways to fix this which are all described in the duplicate. "* I recall my React project where it was quite basic stuff to bind components to each other*" .bind() is not at all related to either React or Angular bindings. It just sets some parameters for a function. At its core this is a JavaScript problem. There is nothing Angular about it. Not knowing it is, doesn't make it new.
    – VLAZ
    Commented Mar 13, 2022 at 12:35
  • @VLAZ I disagree with the React-part. In React (at least the way we resolved the communication between components) required binding the callback. In Angular, I use input/output and services to communicate between components, no binding as we refer to here. You're right that's in the fundamentals, it's a JS matter. But I maintain that's not useful information at where I was standing. Even now, that I resolved it and have a working sample, I still find it a bit odd in Angular (but would in React). But YMMV, of course. At any rate, it was an opportunity to learn something new, which is always +. Commented Mar 13, 2022 at 12:53

1 Answer 1

10

We don't re-open questions just because someone has an answer. The only way to get the question re-opened would be to address the problem(s) that led to it getting closed in the first place. In this case, that is probably possible, since the reason the question was closed was because it lacked a minimal, reproducible example. If you've solved the problem, you surely now have a minimal, reproducible example, so the approach would be:

  • Create and test the minimal, reproducible example; confirm that it reproduces the originally-described problem.
  • Edit this repro code into the original question.
  • When submitting the edit, indicate that the edit resolves the problem(s) noted with the question when it was closed, which will send the question to the "re-open" review queue.
  • With luck, the question will be re-opened via the review queue. With more luck, it will be re-opened without waiting forever. If your luck runs out, you can escalate via Meta, similar to what you've done here, except after having made the edit(s) that fix the problem(s) identified in the close reason(s).

What VLAZ points out about the answer not being particularly useful to others because the question is so commonly asked may well be true—I don't have any idea, because this isn't my subject-matter area. But if it is true, then the question could/should have been closed as a duplicate. Perhaps even still should be: if someone wants to propose a suitable duplicate, then I am happy to change the close reason. (It generally doesn't make sense to waste time editing a question into shape, adding repro code, etc., and re-open it, only to re-close as a duplicate.)

13
  • 1
    The duplicate is: How to access the correct this inside a callback which explains the details of why OP's problem occurs and what the solutions are. How does the "this" keyword work? is partially related as it explains the mechanics around this but is not exactly specific to OP's problem. I'd go with the first one as a duplicate only and I've linked the second one in the comments for additional information.
    – VLAZ
    Commented Mar 13, 2022 at 9:15
  • And... there's nothing unique to Angular or anything else that might be misleading to a programmer, even a novice to these technologies, that isn't covered adequately by that duplicate, @VLAZ? Would it be helpful for Konrad to edit one of the answers there or post one of his own?
    – Cody Gray Mod
    Commented Mar 13, 2022 at 9:18
  • 2
    Nothing unique to Angular. It's just how JS works. calling takesCallback(this.callback) "looses" the reference to this and would execute the method with a different value to it. Which is quite confusing if you expect it to be the current value of this. This answer and this answer directly show off useful generic solutions. As does the top rated answer under the heading "Common problem: Using object methods as callbacks/event handlers" but it also includes a lot of details about why it's a problem.
    – VLAZ
    Commented Mar 13, 2022 at 9:41
  • 5
    Ultimately, I don't see the point of further reinforcing the idea that Angular is somehow different from JavaScript because it is not. It is a JavaScript problem at its core and attempting to mask it just makes it harder to understand. OP expected Angular does some sort of magic for the value of this but it doesn't. Never has. There dupe target is the fourth most frequent question for JS because of how many duplicates there are of it. And even with all the signposts people still don't find it. Setting up a new dupe with different keywords would hardly help in that situation.
    – VLAZ
    Commented Mar 13, 2022 at 9:41
  • 1
    Maybe it would be easier to just get them to fix JavaScript, then :-p
    – Cody Gray Mod
    Commented Mar 13, 2022 at 9:46
  • If Angular or TS actually that, maybe we'd get less of an influx of these questions...
    – VLAZ
    Commented Mar 13, 2022 at 9:53
  • @VLAZ I see your point but I'd like respectfully disagree. Proof being none of my associates ever having used bind in Angular for several years. I believe that you're making the assumption that promises are the dominant way to resolve callbacks in Angular, which isn't correct. In most cases, the observables are applied and the machanics of those doesn't expose the binding the way you present it. Please keep in mind that this is not a JS question. It's an Angular question (as suggested by the tags). And I'll gladly show you how I misunderstood the ambiguous hint in the comments. :) Commented Mar 13, 2022 at 10:29
  • 3
    @KonradViltersten I fully agree with VLAZ. The only thing related to Angular is that you are using TypeScript, which is a superscript of JS and which is compiled to JS. This question really doesn't differ much from the many other questions we get with this same problem. (Like this one or this one or this one or any other from this endless list.)
    – Ivar
    Commented Mar 13, 2022 at 12:15
  • 1
    @KonradViltersten "I believe that you're making the assumption that promises are the dominant way to resolve callbacks in Angular" Never said that ever. The facts are - you had a callback. When you pass a method as reference as in takesCallback(this.callback), it won't preserve the value of this when called. There are literally three ways to fix it - 1. use .bind to store it .this.callback.bind(this) 2. Preserve the calling context () => this.callback() 3. (subtype of 2.) save a reference to this and use it self = this; function() { self.callback()}. No promises.
    – VLAZ
    Commented Mar 13, 2022 at 12:32
  • 1
    @VLAZ I see your point and I want to stress that I do not disagree. In fact, I may very well be confused and simply too... "unfortunate at thinking"... so to speak. Now, that I got it working, I start to see how it fits. However, I still maintain that the information I read in my research (including the links provided) are not sufficient to resolve the issue reasonably quickly (i.e. an hour or two). I'm not claiming that my question was so excellent that it would help others, so you're probably right on re-closing it. But still, that was less than pleasant. I prefer my .subscribe's. Commented Mar 13, 2022 at 12:48
  • 1
    @KonradViltersten "I prefer my .subscribe's." I still don't understand what you're talking about or what you think I am. To be clear - I've never talked about promises or subscriptions or anything of the like - it's just callbacks in the end. Any function that is passed into a call is a callback. And in JS any callback is susceptible to losing the value of this when you pass in a method. Event listeners, subscriptions and even promises themselves take a callback. At no point have I commented on any asynchronous handling of data and I don't know why you seem to think I have.
    – VLAZ
    Commented Mar 13, 2022 at 14:33
  • @VLAZ Firstly, my apology if my wording was perceived as less mannered. Not my intention and I'm sorry if it came out that way. Now, to you question. I suspect that both you and I are assuming something and due to that, we fail to see the other's point. To me, when I talk to Google's API, I request data in an asynchronous way, which in my Angular-prone mind is equivalent'ish to subscribing to an observable. Now, as it's a JS-styled callback, the behavior will differ but it doesn't matter to the reason behind the question. Commented Mar 13, 2022 at 16:22
  • @VLAZ Of course it is relevant due to the reality's bitchy nature. But to someone who's used to Angular'ish approach, that callback is so similar in syntax to the call we get from observables. The mix-up is likely to happen. And the question was ask from the perspective of Angular. The fact that it transpiles to JS has little use to the confused developer. So, in the end, you're perfectly correct in your statement. Still, I believe there's many others who trip over the same (false) assumption. Commented Mar 13, 2022 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .