this

Arrow functions using call, apply, bind - not working? [duplicate]

半腔热情 提交于 2019-12-12 21:24:03
问题 This question already has answers here : When should I use Arrow functions in ECMAScript 6? (9 answers) Can you bind 'this' in an arrow function? (8 answers) Are 'Arrow Functions' and 'Functions' equivalent / exchangeable? (1 answer) Closed 2 years ago . I'm being a bit puzzled when I try to convert a simple ES5 code to ES6. Let's say I have this block of code: var obj = {num: 2} var addToThis = function (a, b, c) { return this.num + a + b + c } // call console.log(addToThis.call(obj, 1, 2, 3

Is it true that using “this.” before the parameters in c# uses more memory?

半城伤御伤魂 提交于 2019-12-12 18:20:56
问题 than just to call the parameter as it is? 回答1: If you mean fields, then no. The compiler injects "this" (ldarg.0) whether you use it explicitly (this.foo) or implicitly (foo). It does, however, take 5 more characters in your source code... so a handful of bytes on your development hard disk. It will make exactly zero difference in the compiled IL or at runtime. There are two scenarios where use of "this" changes things: when there is a variable/parameter with the same name ( this.foo = foo; )

Javascript - how to bind 'this' inside an ajax call to the object literal

倾然丶 夕夏残阳落幕 提交于 2019-12-12 17:29:17
问题 I have an object literal router , containing an ajax call. I want to call other functions this.printMovies() inside the ajax call but this refer to the ajax object. How do I escape it and make this refer to the router object itself? var router = { //... init : function() { this.getData("api/movies", "movies", callback); }, getData : function (url, htmlType, callback) { $.ajax({ url: url, dataType: 'json', success: function (response) { if (response && response.length > 0) { this.printMovies

Javascript “this” losing context in IE

痞子三分冷 提交于 2019-12-12 13:34:50
问题 The following works fine in firefox/safari/chrome, in IE, "this" appears to be losing context in the handleEvent() function...the result of the alert is [object Window], which is not what I want; when output from handleEvent(), "this" needs to be a reference to the HandleClick object, not the Window object. Am I missing something basic which is causing this in IE? <html> <head> <script type="text/javascript"> HandleClick = function(el) { this.element = document.getElementById(el); if( this

Should I reference 'this' in local variable?

霸气de小男生 提交于 2019-12-12 12:08:19
问题 I often see this in code: var me = this; . Why is that? Is there some performance gain if I reference 'this' in local variable? 回答1: It's useful if there are functions inside a function, such that code in those nested functions needs access to the value of this from the outer context. function example() { var me = this; document.getElementById('whatever').onclick = function() { me.clicked = 1; }; } Because this is established anew for every function call, without stashing the outer this in a

Proper use of this->

牧云@^-^@ 提交于 2019-12-12 11:53:00
问题 What is the proper use of the this self-reference of a class? I sometimes use it inside a method to clear out that the variable used is a member variable and not one declared inside the method, but on the other side I am wondering if this is a good reason to do so, as I think you should always code (and comment) in a way which is self-explaining and therefore would make such an unneeded use of this unnecessary and another reason against it would be that you are actually producing more code

Javascript: attaching OOP methods to events and the 'this' keyword

荒凉一梦 提交于 2019-12-12 11:44:17
问题 I am new to OOP Javascript and am having trouble with the this keyword and events. What I'm trying to achieve is: I have multiple DOM objects and want to not only bind a common event to them, but keep some data about the aforementioned objects in a global container (to increase runtime performance). So what I do is basically this: function ClassThatDoesSomething() { /* keeps node ids for processing in this.init */ this.nodes = new Array(); /* keeps processed node data for fast access */ this

Template function is_same in template classes

一世执手 提交于 2019-12-12 11:02:29
问题 Why this code produces a false output? //this-type.cpp #include <iostream> #include <type_traits> using namespace std; template<typename testype> class A { public: A() { cout << boolalpha; cout << is_same<decltype(*this), A<int>>::value << endl; } }; class B : public A<int> { }; int main() { B b; } Output: $ g++ -std=c++11 this-type.cpp $ ./a.out false The type of "*this" inside A through B is A< int >, isn't it? 回答1: *this is an lvalue of type A , so decltype(*this) will give the reference

Access member field with same name as local variable (or argument)

寵の児 提交于 2019-12-12 09:41:19
问题 Consider following code snippet: struct S { S( const int a ) { this->a = a; // option 1 S::a = a; // option 2 } int a; }; Is option 1 is equivalent to option 2? Are there cases when one form is better than another? Which clause of standard describes these options? 回答1: option 1 is equivalent to option 2, but option 1 will not work for a static data member EDITED: static data members can be accessed with this pointer. But this->member will not work in static function. but option 2 will work in

Java: Alternative to passing “this” as constructor argument in order to reference creating object

谁说胖子不能爱 提交于 2019-12-12 07:51:39
问题 I've spent a while thinking about different solutions that the one I went for as I've read around (I am not really experienced with Java yet) that using this for a constructor argument isn't usually a good practice. What I am trying to do is to instantiate several objects of class JobGroupMod and for every JobGroupMod I have to create a certain number of JobMod objects that must be able to reference back the JobGroupMod objects in which they've been spawned from. In order to accomplish that I