this

Save access to this scope

▼魔方 西西 提交于 2019-11-26 21:07:49
I have color stored in a data attribute on my button that I wanted to use in a toggle. However, when I tried to access the data information using this , no data was available. How can I keep my access to the correct this scope? I was trying to only toggle the given color for the elements which didn't contain Skip. html <div> <input id="toggleButton" type="button" value="Toggle" data-color="Red" /> </div> <div id="toggleSet"> <div>Element</div> <div>Skip</div> <div>Element</div> </div> css .ActivateRed{ color: red; } js $('#toggleButton').click(function(){ $("#toggleSet div").each(function

What object javascript function is bound to (what is its “this”)?

≯℡__Kan透↙ 提交于 2019-11-26 21:00:48
I know that inside the function it is this . var func = function { return this.f === arguments.callee; // => true, if bound to some object // => false, if is bound to null, because this.f === undefined } var f = func; // not bound to anything; var obj = {}; obj1.f = func; // bound to obj1 if called as obj1.f(), but not bound if called as func() var bound = f.bind(obj2) // bound to obj2 if called as obj2.f() or as bound() Edited: You can't actually call obj2.f() as f doesn't become a property of obj2 edit end. The question is: how to find the object, that the function is bound to, outside of

Why doesn't this closure have access to the 'this' keyword? - jQuery

こ雲淡風輕ζ 提交于 2019-11-26 20:56:11
问题 I'm a beginner to closures (and Javscript in general), and I can't find a satisfactory explanation as to what's going on in this code: function myObject(){ this.myHello = "hello"; this.myMethod = do_stuff; } function do_stuff(){ var myThis = this; $.get('http://example.com', function(){ alert(this.myHello); alert(myThis.myHello); }); } var obj = new myObject; obj.myMethod(); It will alert 'undefined' and then 'hello'. Obviously this should not be jQuery specific, but this is the simplest form

Preserve 'this' reference in javascript prototype event handler [duplicate]

痞子三分冷 提交于 2019-11-26 20:43:22
This question already has an answer here: Preserving a reference to “this” in JavaScript prototype functions [duplicate] 7 answers What is the correct way to preserve a this javascript reference in an event handler stored inside the object's prototype? I'd like to stay away from creating temp vars like '_this' or 'that' and I can't use a framework like jQuery. I saw a lot of people talk about using a 'bind' function but was unsure of how to implement it in my given scenario. var Example = function(foo,bar){ this.foo = foo; this.bar = bar; }; Example.prototype.SetEvent = function(){ this.bar

What is the 'this' pointer?

好久不见. 提交于 2019-11-26 20:10:11
I'm fairly new to C++, and I don't understand what the this pointer does in the following scenario: void do_something_to_a_foo(Foo *foo_instance); void Foo::DoSomething() { do_something_to_a_foo(this); } I grabbed that from someone else's post on here. What does this point to? I'm confused. The function has no input, so what is this doing? TelKitty this refers to the current object. The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A , and class A has a non-static member function f() . If you call the function x.f() , the keyword this in

Why can we use 'this' as an instance method parameter?

耗尽温柔 提交于 2019-11-26 19:51:34
问题 What is receiver parameter in Java ? Java 8 Language Specification talks about this . 回答1: The JLS gives a hint: Either way, the receiver parameter exists solely to allow the type of the represented object to be denoted in source code, so that the type may be annotated. These two methods are equivalent: class Test { void m1() { } void m2(Test this) { } } However the latter allows you to add annotations: void m2(@MyAnnotation Test this) { } //where MyAnnotation can be defined like this for

What are all the differences between function and constructor function in JavaScript?

喜欢而已 提交于 2019-11-26 19:45:51
问题 In this blog author says below function is a constructor function : function Cat(name, color) { this.name = name; this.color = color; } Cat.prototype.age = 0; var catC = new Cat("Fluffy", "White"); The instances of Cat function has a name and colour property. Is this the only difference between normal and constructor function? 回答1: A constructor function is a normal function. What makes the difference here is the use of the new operator which makes the context ( this ) in the function the new

How can I exclude $(this) from a jQuery selector?

梦想的初衷 提交于 2019-11-26 19:22:59
I have something like this: <div class="content"> <a href="#">A</a> </div> <div class="content"> <a href="#">B</a> </div> <div class="content"> <a href="#">C</a> </div> When one of these links is clicked, I want to perform the .hide() function on the links that are not clicked. I understand jQuery has the :not selector, but I can't figure out how to use it in this case because it is necessary that I select the links using $(".content a") I want to do something like $(".content a").click(function() { $(".content a:not(this)").hide("slow"); }); but I can't figure out how to use the :not selector

Why does String.prototype log it's object like a standard object, while Array.prototype logs it's object like a standard array?

不问归期 提交于 2019-11-26 18:40:47
问题 Simply why does String.prototype log the string object with the standard curly brackets and key value pairs, and the Array.prototype log the array object just like an array, with square brackets and values? String.prototype.test = function(){ console.log(this); // logs { '0': 't', '1': 'e', '2': 's', '3': 't' } }; var str = 'test'; str.test(); Array.prototype.test1 = function(){ console.log(this); // [1,2,3,4] }; var arr = [1,2,3,4]; arr.test1(); 回答1: Because in a method call the this

Should I use “this” keyword when I want to refer to instance variables within a method?

十年热恋 提交于 2019-11-26 18:27:44
问题 My teacher says that when I try to access an instance variable within a method I should always use the this keyword, otherwise I would perform a double search. A local scope search and then an instance scope search. Example: public class Test(){ int cont=0; public void Method(){ System.out.println(cont);//Should I use This.cont instead? } } I hope he is wrong, but I can't find any argument. 回答1: No, only use this when you have a name conflict such as when a method parameter has the same name