this

What's the difference between $(this) and this in jQuery?

懵懂的女人 提交于 2019-11-28 09:43:25
What's the difference between $(this) and this in jQuery, and why do they sometimes give the same result and other times behave differently? $(this) wraps this with the jQuery functionality. For example, this code would fail: $('.someDiv').onClick(function(){ // this refers to the DOM element so the following line would fail this.fadeOut(100); }); So we wrap this in jQuery: $('.someDiv').onClick(function(){ // wrap this in jQuery so we can use jQuery fadeOut $(this).fadeOut(100); }); $(this) decorates whatever object this points to with the jQuery functions. The typical use case is for this to

How does “use strict” modify the rules for “this” in Javascript?

假装没事ソ 提交于 2019-11-28 09:37:08
问题 I'm trying to understand what rule for "this" that "use strict"; modifies in the below case. After reading (http://unschooled.org/2012/03/understanding-javascript-this/) my best guess is that since the functon isStrictModeOn() is not "attached" to anything, this refers to null. Which is suppose to be a more sensible alternative to Javascript just attaching the this to the global object. Is that the correct interpretation of the change that "use strict" is making in this case? http://www

Node.js: What is the context of the `this` operator when used in module scope? [duplicate]

邮差的信 提交于 2019-11-28 09:19:36
问题 This question already has an answer here: What does “this” mean in a nodejs module? 1 answer The code I write the following code and save it as test.js: var foo = 'I am local'; global.foo = 'I am global'; function print () { console.log(this.foo); }; print(); console.log (this.foo); I then run it in the terminal with the command node test.js and it returns: I am global undefined The question Why does it not return: I am global I am global ? 回答1: Inside a Node module, this by design refers to

With TypeScript: unable to refer to 'this' (class) from inside a function

半城伤御伤魂 提交于 2019-11-28 09:15:47
问题 I'm learning TypeScript and have the following class: class DetailDriver { public get driver() { return super.getEntity(); } public activate(): breeze.Promise { var id = this.driver.id(); // this refers to (class) DetailDriver return promise .then(getCertificate) .fail(somethingWrong); function getCertificate() { var id = this.driver.id(); // this refers to any return ... } } } As you can see on the above code, the first call to this refers to my class DetailDriver . That's good. The second

Why I can't directly set console.log() as callback function

血红的双手。 提交于 2019-11-28 09:08:44
问题 Why this code doesn't work function callback(num, func) { for(var i = 0; i < num; i++) { func(); } } callback(4, console.log("Hello")); I know I have to do like this: callback(4, function() { console.log("hello"); }); But I still don't understand the reason why I have to do like that. 回答1: Lets step through it to help you visualise what is going on. When the interpreter reads your code, it evaluates function expressions inside-out. That is to say: callback(4, console.log("Hello")); // ^------

Why do I have to use $(this)? [duplicate]

陌路散爱 提交于 2019-11-28 09:06:49
Possible Duplicate: jQuery $(this) vs this In jquery sometimes I find that within a function I have to use $(this) because this won't work: var listItems = $('li'); listItems.each(function(index) { $(this).css({ }) }) Any ideas as to the reason why? jAndy $() is the jQuery constructor function this is a reference to the DOM element of invocation So basically, you're turning a DOM reference into a jQuery object In your example, you could also use listItems.each(function(index, element){ $(element).css({ }); }); ..since .each() will pass both, index + element in its callback. this is the native

this value is null in function (React-Native)

时光怂恿深爱的人放手 提交于 2019-11-28 09:02:45
As per local testing, 'this' seems to be null inside the row render function. As a result this prevents me from binding a local function on the onPress prop. I have this render block: render() { return ( <ListView dataSource={this.state.dataSource} renderRow={this._renderRow} renderHeader={this._renderHeader} style={styles.listView} /> ); } and a local function _visitEntryDetail() { console.log('test'); } then row render _renderRow(something) { return ( <TouchableHighlight style={[styles.textContainer, filestyle.container]} onPress={this._visitEntryDetail.bind(this)} > <View> <Text style=

How can I access `this` in an event handler?

馋奶兔 提交于 2019-11-28 08:53:50
问题 I have a class which creates a DOM element and has to capture all click events. Simplified code: function myClass() { this.domElement = document.createElement("canvas"); this.domElement.addEventListener("click", this.handleClick); } myClass.prototype.handleClick = function(evt) { alert("Clicked!"); // How to modify `this` object? } Now I want to modify some attributes and variables of the myClass instance in handleClick() . But this refers to the canvas object, of course. Question: How can I

Should I use `this` within a class?

人盡茶涼 提交于 2019-11-28 08:14:48
Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember ? What is considered better style? Is there any performance difference? (I am not talking about the case where a local variable has the same name as the data member, in which case you must, to my knowledge, use this-> to distinguish between them.) As a general rule, it's a question of local conventions. Most of the places I've seen do not use this-> except when necessary, and that's the convention I prefer as well, but I've heard of people who prefer to use it systematically.

How does 'this' work in JavaScript?

馋奶兔 提交于 2019-11-28 08:14:40
问题 I know there are several other posts on this topic but they still leave me confused. I've included jQuery and everything and, I have a simple javascript class like this example: function CarConstructor(){ this.speed=19; // in mph this.make="Ford"; this.fillKph=fillKph; } function fillKph(){ $("#kphdiv").html(this.speed*1.61); } car1 = new CarConstructor(); car1.fillKph(); Now I know that that code snippet doesn't work and is not properly consturcted. The "this" keyword there is referencing my