this

this vs $(this) [duplicate]

白昼怎懂夜的黑 提交于 2019-11-29 21:36:42
Possible Duplicate: jQuery $(this) vs this I'm new to this and trying to get my concept right. There has been many instances of the use of " this " and " $(this) ". Can someone please explain the difference and in what condition that we use the two different "this"? In jQuery functions, this most often refers to the actual DOM element you're dealing with, whereas $(this) returns a jQuery object that wraps the element. In JavaScript, this always refers to the current scope. Many of jQuery's functions will set that scope to be the element you're working with. For instance $("#someElement").click

addEventListener and the scope of this

时间秒杀一切 提交于 2019-11-29 18:32:05
问题 I have a third party flash object which i can manipulate through a javascript API they provided. I am tryind to listen to an event on this object and then fire event inside my object to further bubble up the event. I happen to being using EXT Js but i dont think its important here. Sample code this.chart.addEventListener('create', function() { this.fireEvent('created'); }, false) My problem is that 'this' inside the anonymous function refers to the object that fired the event rather than my

How does JavaScript determine when to give a function call a “this” context? [duplicate]

徘徊边缘 提交于 2019-11-29 17:08:32
问题 This question already has answers here : Cases where 'this' is the global Object in Javascript (4 answers) Closed 5 years ago . For obvious reasons, in JavaScript, the following two calls are different: foo.bar(); var bar = foo.bar; bar(); Namely, in the first call, this is the foo object. In the second, it's a reference to the global scope. However, the following example is a little less intuitive: (foo.bar)(); I would expect it to operate the same way as the second example, but it actually

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

做~自己de王妃 提交于 2019-11-29 15:59:13
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.novogeek.com/post/ECMAScript-5-Strict-mode-support-in-browsers-What-does-this-mean.aspx function

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

本小妞迷上赌 提交于 2019-11-29 15:48:57
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 ? Felix Kling Inside a Node module, this by design refers to module's exports object: console.log(this === exports); // true Making console.log(this.foo)

React.js - “this” undefined even after binding

旧巷老猫 提交于 2019-11-29 15:46:36
I am trying to capture onChange event of the input and calling setState with the new value, but as soon as I type in the input I get: Uncaught TypeError: Cannot read property 'setState' of undefined Even though I have called this.handleChange.bind(this) in the constructor index.js import React from 'react' import * as ReactDOM from "react-dom"; import App from './App' ReactDOM.render( <App />, document.getElementById('root') ); App.js import * as React from "react"; export default class App extends React.Component { constructor(props) { super(props) this.handleChange.bind(this) this.state =

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

时间秒杀一切 提交于 2019-11-29 15:25:12
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 call to this (inside getCertificate ) refers to any . That's not what I need. I need to refer to my

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

坚强是说给别人听的谎言 提交于 2019-11-29 14:31:56
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 access this of an object in an event handler? This can be accomplished via closing over a reference to

How can I keep the context of 'this' in jquery

混江龙づ霸主 提交于 2019-11-29 14:27:25
问题 I have something like this: var Something = function(){ this.render = function(){}; $(window).resize(function(){ this.render(); }); } The trouble is that inside the anonymous function 'this' refers to the window object. I know I could do something like: var Something = function(){ this.render = function(){}; var tempThis = this; $(window).resize(function(){ tempThis.render(); }); } but is there a better way? This doesn't look very elegant. 回答1: The solution you found is the the one most

Ruby equivalent to PHP's $this

送分小仙女□ 提交于 2019-11-29 14:26:40
问题 What is the equivalent of PHP's $this-> in Ruby? 回答1: The ruby equivalent of this is self - they both refer to the current instance. The tricky part is that in Ruby class scope, self refers to the current instance of the class Class that defines the class you are building. Inside a method, self refers to the instance of the class. eg: class Example puts self # => "Example" - the stringified class object def foo puts self # #<Example:0xdeadbeef> - the stringified instance end end 回答2: The