this

Does the comma operator influence the execution context in Javascript?

我怕爱的太早我们不能终老 提交于 2019-12-27 22:02:15
问题 var a = 1; var b = { a : 2, c : function () { console.log(this.a); } }; b.c(); // logs 2 (b.c)(); // logs 2 (0, b.c)(); // logs 1 The first is understandable, for "this" is pointed to Object "b". But why does the second one log the same result? I thought "this" should be pointed to the global execution context. And the third one, it seems that the comma operator influences the execution context. 回答1: You really have a nice corner case there! My take on it: the first is straightforward. Just a

jQuery/JavaScript “this” pointer confusion

北慕城南 提交于 2019-12-27 20:08:51
问题 The behavior of "this" when function bar is called is baffling me. See the code below. Is there any way to arrange for "this" to be a plain old js object instance when bar is called from a click handler, instead of being the html element? // a class with a method function foo() { this.bar(); // when called here, "this" is the foo instance var barf = this.bar; barf(); // when called here, "this" is the global object // when called from a click, "this" is the html element $("#thing").after($("

What is the difference between Class.this and this in Java

不打扰是莪最后的温柔 提交于 2019-12-27 11:00:13
问题 There are two ways to reference the instance of a class within that class. For example: class Person { String name; public void setName(String name) { this.name = name; } public void setName2(String name) { Person.this.name = name; } } One uses this.name to reference the object field, but the other uses className.this to reference the object field. What is the difference between these two references? 回答1: In this case, they are the same. The Class.this syntax is useful when you have a non

Why calling array.prototype.forEach.call() with an array set to THIS object not working

笑着哭i 提交于 2019-12-25 18:38:25
问题 Here I have a number array. I tried to test if Array.prototype.forEach can be used on array in a different way than the traditional way. In traditional way we pass the THIS argument as second parameter to forEach . Here I used Array.prototype.forEach.call() and for this I used the array as argument to call method. But this indication the window object. Why is that ? number=[1,2,3,4,5]; Array.prototype.forEach.call(number,function(elem){ console.log(this); }); 回答1: Because assuming forEach has

Difference between this and $(this) and callback function on event using jQuery

梦想的初衷 提交于 2019-12-25 12:50:15
问题 Learning jQuery and I have a couple of questions. I am wondering why this won't work: $('li').html(function(){ return '<em>' + this.text() + '</em>'; }); But this will: $('li').html(function(){ return '<em>' + $(this).text() + '</em>'; }); And also, why is this working: $(document).on('load', alert('done')); But not this: $(document).on('load', function(){alert('done'); }); Thanks, 回答1: this and $(this): In the first example, this is a node element, and since node elements don't have a

this.setState() is not a function when using react with jquery inside componentDidmount [duplicate]

好久不见. 提交于 2019-12-25 09:13:52
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 3 years ago . When I use react with es6 and jquery's ajax function, I got this.setState() is not a function error. I tried bind this within constructor using this.componentDidmount = this.componentDidmount.bind(this); , but still not working. Can any one help me? Thanks! Here is my code: import React from 'react'; import $ from 'jquery'; class UserGist extends React.Component {

click function, css add class only on this item

最后都变了- 提交于 2019-12-25 08:09:46
问题 I have a grid with an image being grey, on hover the image get colorize (the image can not be setup as background image for information). When Hover the image, a text appear, on click on this, I have a content which toggle. Here are my two issues: 1 On toggle, only the image of the item have click should be colorize, actually they all get colorize unfortunatly. ( test on the first item of the secodn row) I tried to use $(this).prev without success. ( see the code) //** HERE THE TOGGLE

Getting an #id from jQuery's this

孤者浪人 提交于 2019-12-25 06:38:53
问题 How do I get the ID associated with the this keyword? I've tried several things from SO pages but here is where I've decided to open the question: $('.anySelector').waypoint(function() { console.log(this); // entire selector's content var thisContent = this; console.log($(thisContent).attr('id')); // undefined }); 回答1: If this really is a DOM Element (which is very probable using jQuery) it is as simple as just accessing the id property of the element. jQuery( '.anySelector' ).waypoint(

In an abstract class does the “this” keyword refrerence the parent or child class?

我是研究僧i 提交于 2019-12-25 05:32:13
问题 I have an abstract class Flight. Flight contains the method schedule() which calls the private method schedule(final Flight f) public void schedule() { schedule(this); } private void schedule(final Flight f) { new Timer().schedule(new TimerTask() { @Override public void run() { f.checkIn(); updateList(); } }, this.getDate()); } Lets now say I have a class SouthWestFlight that extends Flight Flight f = new SouthWestFlight(); //ignore the missing params doesn't matter for example f.schedule();

“this” inside event handler from HTML attribute

本小妞迷上赌 提交于 2019-12-25 03:15:55
问题 I thought I understood wel the "this" keyword until I saw this code : <body> <button onclick="go()">clic1</button> <button id="btn">clic2</button> <script> function go() { console.log(this); } var btn = document.getElementById("btn"); btn.onclick = function() { console.log(this) } </script> </body> I have a HTML document with two buttons that do the same thing when clicked :they log the "this" keyword. I'm very surprised they don't show the same result : For the button "clic1" : this = Window