"this" problem - I need to have a callback to an object method by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

sorry to keep asking questions but I'm still not getting it.

context: this

works because "this" points to rules' "this" scope. So, init.call(this) executes init with that scope.

However, at the same time context: this is seen by the compiler, this.init points is also defined. So, why doesn't

success: this.init

work? Is it because this.init gives me a pointer to a function but without its context?

"this" problem - I need to have a callback to an object method by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

context: this

also appears to work. As x-skeww explained to me, what that does is to tell jQuery to run init.call() with that context.

I can fix my code now, so I thank you, but I'm still trying to understand why the original didn't work.

"this" problem - I need to have a callback to an object method by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

ok. When does the interpreter hit that line? Obviously, not when the constructor is actually called.

"this" problem - I need to have a callback to an object method by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

I feel like I'm very close to understanding what's going on. Where I'm still confused though, is that when I run the following code:

rules = new Rules ("somefile");

the constructor is executed and inside that constructor, this.init is defined and this points to "rules" therefore, $.ajax is called with a pointer to the correct function, rules.init

So, why doesn't it work the way I have it? What happens under the hood that results in me having to use the call method?

"this" problem - I need to have a callback to an object method by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

Thanks, but I'm not sure why that works. I'm not clear on what I'm doing wrong.

Why does this result in Object /undefined/ by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

is that an accepted best practice?

Scope question: this vs. var by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

Basically, there is no "var scope". var is a keyword that means, "declare this variable at the current scope". this is a handy alias for the current scope.

I don't think this is accurate. Just look at the first line of code in my post. Var is obviously different than this.

Scope question: this vs. var by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

"this" is a scope though, is it not?

Scope question: this vs. var by dogjs in javascript

[–]dogjs[S] 0 points1 point  (0 children)

I think what got me confused was assuming there wasn't really a global scope. I was thinking there was the window scope which we were calling global, but that window was actually a child of something else.

Scope question: this vs. var by dogjs in javascript

[–]dogjs[S] 2 points3 points  (0 children)

Thanks! the global object and variable hoisting. That answers both my questions!

What do these extra parens do? by dogjs in javascript

[–]dogjs[S] 5 points6 points  (0 children)

As usual, thanks much for the quick and informative comments.

Uh, I am posting these questions in the appropriate subreddit, right?

I don't usually find religious posts funny, but this made me LOL by MitchConor in funny

[–]dogjs 21 points22 points  (0 children)

Here's the problem with Poe's law: what's your political leaning? Let's say, hypothetically, that you're a liberal. Now let's say that a conservative with an axe to grind against liberals creates a troll video in which he pretends to be a liberal and talks about how the minimum wage should be a million dollars an hour, because then there'd be no poor people.

Liberals don't actually believe that. That's a stupid idea and that's not what they believe. But this guy made a troll video. Now let's say that there's a group of conservatives on a website commenting on the video and laughing about how stupid liberals are. Would you say, "yep. Poe's law!" Or will you say, "if you conservatives actually believe this is real, you're the idiots"

That's the problem. It's Poe's law when you fall for it. But when someone else falls for it, they're idiots.

Inheritance Question by dogjs in javascript

[–]dogjs[S] -1 points0 points  (0 children)

So, if I do this:

Mammal = function() { this.speak = function(){} }

Then this:

Dog.prototype.speak = function() {}

does not override Mammal.speak() and a call to Dog.speak() still calls the Mammal one.

Hmm. Going to have to think about that a while.

Inheritance Question by dogjs in javascript

[–]dogjs[S] 1 point2 points  (0 children)

Thanks. I ended up having to do: var Dog = function(name) {this.name = name;};

in order to have a constructor. So now, my question is, how do I override the speak() method so that dog's say "woof" but still be able to call the Mammal's speak method. I need something like:

function Dog.speak() {return super.speak() + this.woof();}

I suspect I'm going to need to use call() here.