this

Calling a class function in forEach: how Javascript handles “this” keyword

…衆ロ難τιáo~ 提交于 2020-01-13 03:13:15
问题 I'm new to Javascript and just want to make sure I'm understanding how it handles the this keyword, since... well, it seems like it's pretty messy. I've checked out similar questions on StackOverflow and want to make sure I'm not moving forward with the wrong idea. So I'm defining a class like so, and want to process every point received in the constructor. function Curve(ptList) { this.pts = []; if(ptList.length > 2) { // I want to call "addPoint" for every item in ptList right here } }

How to pass $(this) in success callback of $.ajax

試著忘記壹切 提交于 2020-01-12 18:37:19
问题 I have seen a few different examples of accessing $(this) is the success callback of ajax but none give me the answer I want - they all access $(this) within the ajax function, I want to pass $(this) to a separate function. So if there are 2 textboxes that need to be validated $("#tb1").focusout(function(){ $.ajax({ type:'POST', url: 'validURL', data: {various_parameters}, contentType: 'application/json; charset=utf-8', dataType:'json', success: function(data){ validInput(data, $(this)); },

How to pass $(this) in success callback of $.ajax

╄→гoц情女王★ 提交于 2020-01-12 18:35:09
问题 I have seen a few different examples of accessing $(this) is the success callback of ajax but none give me the answer I want - they all access $(this) within the ajax function, I want to pass $(this) to a separate function. So if there are 2 textboxes that need to be validated $("#tb1").focusout(function(){ $.ajax({ type:'POST', url: 'validURL', data: {various_parameters}, contentType: 'application/json; charset=utf-8', dataType:'json', success: function(data){ validInput(data, $(this)); },

Why GCC 5.3.0 gives warning when binding reference to “this” pointer

∥☆過路亽.° 提交于 2020-01-12 12:51:44
问题 Here is the minimal example: class A { A* const& this_ref; public: A() : this_ref(this) {} }; GCC 5.3.0 gives warning: warning: a temporary bound to 'A::this_ref' only persists until the constructor exits [-Wextra] A() : this_ref(this) {} Is this a temporary then? What the... MSVC 2015 is silent about this, and referring to class members by this_ref->member outside the constructor in my case gives expected behaviour (but might be just a case of UB, not sure). EDIT: Note this question extends

Is `this` allowed inside a noexcept specification?

独自空忆成欢 提交于 2020-01-11 08:17:22
问题 I have some code which requires me to use *this , but I want it to be noexcept friendly: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() noexcept(noexcept(::do_something(*this))) { ::do_something(*this); } }; However, gcc rejects this: <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn()

Is `this` allowed inside a noexcept specification?

不打扰是莪最后的温柔 提交于 2020-01-11 08:17:05
问题 I have some code which requires me to use *this , but I want it to be noexcept friendly: struct foo; // Would actually be something with conditional noexcept void do_something(foo&); struct foo { void fn() noexcept(noexcept(::do_something(*this))) { ::do_something(*this); } }; However, gcc rejects this: <source>:7:43: error: invalid use of 'this' at top level noexcept(noexcept(::do_something(*this))) If I just access a member, gcc is fine: void do_something(int); struct bar { int x; void fn()

Angular: Giving a component field a reference to a service function and calling it from template not working as expected

丶灬走出姿态 提交于 2020-01-11 07:29:28
问题 In my Plunker here (modified Tour of Heroes app from official docs) I created this method in the hero.service doHeroesExist(): boolean { console.log("doHeroesExist called..", this.heroesExist); alert("doHeroesExist called.." + JSON.stringify(this.heroesExist)); return this.heroesExist; } and use it in the app.component class ngOnInit(): void { //this.getHeroes(); this.heroesExist = this.heroService.doHeroesExist; console.log("app ngOnInit called...", this.heroesExist); } as call the

Jquery AJAX call: $(this) does not work after success

混江龙づ霸主 提交于 2020-01-10 04:21:06
问题 I am wondering why $(this) does not work after a jQuery ajax call. My code is like this. $('.agree').live("click", function(){ // use live for binding of ajax results var id=($(this).attr('comment_id')); $.ajax({ type: "POST", url: "includes/ajax.php?request=agree&id="+id, success: function(response) { $(this).append('hihi'); } }); return false; }); Why doesnt the $(this) work in this case after ajax call? It would work if I use it before the ajax but no effect after. 回答1: In a jQuery ajax

Why does JSLint forbid the “this” keyword?

独自空忆成欢 提交于 2020-01-10 04:10:42
问题 Consider this simple example: "use strict"; var Foo = { field: 0, func: function () { this.field = 4; } } JSLint throws the error: Unexpected 'this'. At the line "this.field = 4". I've seem some questions here in StackOverflow asking for this, and in all the cases, the answer was just to enable the "Tolerate this" flag. However, I'm interested in why do the JSLint creators think the use of "this" is (or might lead to) an error. Also, how would I implement member functions without the "this"

Using this.setState in a callback

给你一囗甜甜゛ 提交于 2020-01-09 11:44:10
问题 I have the following code getting a twitter timeline in a react component: componentWillMount: function() { twitter.get('statuses/user_timeline', function(error, data) { this.setState({tweets: data}) }); } But I can't set the state there, because this isn't set to the component in that callback function. How can I set the state within that callback? n.b. console.log(data) instead of this.setState works fine, which is why I suspect the problem is with the this variable. 回答1: You can set this