this

JQuery $(this) selector function and limitations

假装没事ソ 提交于 2019-11-27 13:45:57
I need help understanding $(this). Is it possible to narrow the focus of 'this' within the parentheses or does "this" preclude the use of any other attributes? For example: I don't understand why this code: $(this).children("div") can't be rewritten like this: $(this +" div") without having to resort to something like: $('#'+$(this).attr("id")+" div") Also, can you point me to 'this' in the JQuery documentation? It is difficult to use 'this' as a search term for obvious reasons. this isn't a jQuery "thing", but a basic JavaScript one . It can't be re-written the way you have in examples

onClick Function “this” Returns Window Object

孤街醉人 提交于 2019-11-27 12:52:58
I've come across a head scratching issue with my JavaScript application. If I write an element like this: <li onClick="alert(this.tagName)"></li> I get "LI." However if I do this: <li onClick="foo()"></li> Where "foo()" is: function foo(){ alert(this.tagName); } I get "undefined." I am away how "this" is supposed to work in regards to attached functions. But, I am baffled because "this" is not picking up the element, but apparently defaulting to "window." I can't figure out why this is happening. Does anyone have an explanation? That's because you aren't passing a reference to this in the

Can 'this' ever be null in Javascript

谁说胖子不能爱 提交于 2019-11-27 12:46:17
问题 I have a function along the lines of the following: doSomething: function () { var parent = null; if (this === null) { parent = 'some default value'; } else { parent = this.SomeValue(); } } Could parent ever be set to 'some default value' or is the check for null superfluous? Alternatively, what if I used the less restrictive: doSomething: function () { var parent = this ? this.SomeValue() : 'some default value'; } Could parent ever be set to 'some default value' in this case? 回答1: In non

What's the difference between this and Activity.this

吃可爱长大的小学妹 提交于 2019-11-27 12:44:13
For example Intent intent = new Intent(this, SecondActivity.class); eclipse error: The method setClass(Context, Class) in the type Intent is not applicable for the arguments (FirstActivity.ClickEvent, Class) Intent intent = new Intent(FirstActivity.this, SecondActivity.class); But that will be correct. Anybody can explain the difference between those two ? Thanks. this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to. Activity.this points to the instance of the Activity you are currently in. When you are

What is the 'global' object in NodeJS

大憨熊 提交于 2019-11-27 12:35:44
I've just seen a weird behaviour of the this keyword in NodeJS environment. I'm listing them with code. I've run this codes with NodeJS v6.x , with a single JavaScript file. While testing with one line of code as follows, whether with or without the 'use strict' statement, this points to an empty object {} . console.log(this) But, when I'm running the statement within a self executing function like, (function(){ console.log(this); }()); It's printing a really big object. Seems to me the Global execution context object created by NodeJS environment. And while executing the above function with a

Is it safe to use the “this” pointer in an initialization list?

孤者浪人 提交于 2019-11-27 12:17:25
I have two classes with a parent-child relationship (the Parent class "has-a" Child class), and the Child class has a pointer back to the Parent . It would be nice to initialize the parent pointer upon construction of the child, as follows: class Child; class Parent; class Child { public: Child (Parent* parent_ptr_) : parent_ptr(parent_ptr_) {}; private: Parent* parent_ptr; }; class Parent { public: Parent() : child(this) {}; private: Child child; } Now, I know people recommend not using this in initialization list, and C++ FAQ says I'm gonna get a compiler warning (BTW, on VS2010, I don't get

return “this” in C++?

纵然是瞬间 提交于 2019-11-27 11:57:51
问题 In Java you can simply return this to get the current object. How do you do this in C++? Java: class MyClass { MyClass example() { return this; } } 回答1: Well, first off, you can't return anything from a void -returning function. There are three ways to return something which provides access to the current object: by pointer, by reference, and by value. class myclass { public: // Return by pointer needs const and non-const versions myclass* ReturnPointerToCurrentObject() { return this; } const

“this” inside object [duplicate]

末鹿安然 提交于 2019-11-27 11:01:38
This question already has an answer here: Self-references in object literals / initializers 23 answers I'm trying to calculate a proportional height (while excluding a static height element) from a width that gets passed in via a request (defaults to 560). However, wF.h evaluates to NaN . If I replace this.w with 560 it works, but not when trying to reference the w property of wF . var wF = { w : 560, h : (312 - 42) / (560 / this.w) + 42 }; What gives? I refuse to use two plain vars in succession, because I'm trying to get nice code out of JS. Update: Thanks to everyone who helped explain and

The reason to use JS .call() method?

泄露秘密 提交于 2019-11-27 10:29:27
I'm interested what's the reason to have call() method in JS. It seems it duplicates usual method of calling this . For example, I have a code with call(). var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " " + did_what + " " + what); } f.call(obj, "ate", "food"); The output is "Dog ate food". But the same result I can get assigning the function to the object. var obj = { objType: "Dog" } f = function(did_what, what) { alert(this.objType + " " + did_what + " " + what); } obj.a = f; obj.a("ate", "food"); The result is the same. But this way is more understandable

Difference in context this and getContext()

狂风中的少年 提交于 2019-11-27 10:17:51
问题 What is difference between this and getContext() , when I say this I mean this within an Activity . 回答1: In general there are two type of classes. Ones that extend ContextWrapper class ( Activity , Service , Application ) and those that do not extend it (like View ). If class extends ContextWrapper then you can use this as Context . Such classes normally do not have getContext() method. Those classes that do not extend ContextWrapper but still save and use Context normally expose getContext()