this

How can I use multiple constructors to remove duplicated code while maintaining readability?

泄露秘密 提交于 2019-12-03 00:58:00
int a, b, c; Constructor() { a = 5; b = 10; c = 15; //do stuff } Constructor(int x, int y) { a = x; b = y; c = 15; //do stuff } Constructor(int x, int y, int z) { a = x; b = y; c = z; //do stuff } To prevent duplication of "stuff" and a few assignments, I tried out something like: int a, b, c; Constructor(): this(5, 10, 15) { } Constructor(int x, int y): this(x, y, 15) { } Constructor(int x, int y, int z) { a = x; b = y; c = z; //do stuff } This works for what I want to do, but sometimes I need to use some lengthy code to create new objects or do some calculations: int a, b, c; Constructor():

Why can't IIFE/function access “this” in outer function? [duplicate]

老子叫甜甜 提交于 2019-12-02 23:35:57
问题 This question already has answers here : How does the “this” keyword work? (22 answers) Closed 5 years ago . In the following code, var myObject = { foo: "bar", func: function() { var self = this; console.log("outer func: this.foo = " + this.foo); console.log("outer func: self.foo = " + self.foo); (function() { console.log("inner func: this.foo = " + this.foo); console.log("inner func: self.foo = " + self.foo); }()); } }; myObject.func(); It prints this in the console: outer func: this.foo =

How to “insert” code before this(…) or super(…)?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 22:19:02
问题 Is there any way to implement preliminary calculations before an invocation of super(...) or this(...) constructor? Consider the following example: public class Test { private final int n; private final int m; private final int[] store; public Test(int n, int m) { /* This is common (most generic) constructor of the class Test. It is desirable to invoke it via this(...) call from any other constructor of this class since it contains some common initialization tasks, which are better to

Backbone.js with Google Maps - problems with this and listeners

心已入冬 提交于 2019-12-02 21:16:44
I have a module I created for Google Maps v3 that I'm trying to convert into a Backbone.js view constructor. Here's my view module so far: I'll explain the problems I'm having after the code: pg.views.CreateMap = Backbone.View.extend({ tagName: "div", className: "map", events: {}, latitude: "-23.56432", longitude: "-46.65183", initialize: function() { _.bindAll(this, 'render', 'dragMarker', 'dragMap'); this.latlng = new google.maps.LatLng(this.latitude, this.longitude); var myOptions = { zoom: 16, center: this.latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; this.map = new google.maps.Map($

How to access this variable inside this function

£可爱£侵袭症+ 提交于 2019-12-02 19:16:43
问题 I have private variables in constructor and public variables in the class . I refer to this variables and functions using this keyword. I am getting undefined if I try to access this variables inside this function. I am new to typescript , how can I access this variable inside this function? Technology : Typescript, Angular 2, Angular 1.6.5, JavaScript admin-company-settings.ts import { Component } from '../../../../common/extentions/to-angular2'; import { Http } from '@angular/http'; export

why use this keyword in constructor function

孤者浪人 提交于 2019-12-02 17:38:32
问题 compare code 1 and code 2, which one is correct? function Rectangle(height, width) { this.height = height; this.width = width; this.calcArea = function() { // why use this here? return this.height * this.width; }; } code 2 I thought it's fine with this : function Rectangle(height, width) { this.height = height; this.width = width; calcArea = function() { return this.height * this.width; }; } 回答1: If you don't use this , calcArea will not be accessible through the object of Rectangle. When you

Is there a functional purpose for setting a variable to 'this'? [duplicate]

巧了我就是萌 提交于 2019-12-02 17:30:23
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 5 years ago . As in, sometimes when I look at code by other people, they will go var self = this; or in jquery for example, go var $self = $(this); is there a particular reason for doing so? 回答1: It preserves the value of this for use in functions defined inside the current function. // Contrived example var myObject = { func: function () { var self = this; setTimeout(bar, 1000);

jQuery Passing $(this) to a Function

自古美人都是妖i 提交于 2019-12-02 16:58:56
I have lines of code like this: $(this).parent().parent().children().each(function(){ // do something }); It works well. But I need to run these lines multiple times. So I have created a function and pass $(this) parameter to a function: myFunc( $(this) ); function myFunc(thisObj) { thisObj.parent().parent().children().each(function(){ // do something }); } But in this way, It didn't work. erimerturk you can check this link. http://jsfiddle.net/zEXrq/38/ $("#f").click(function() { myFunc($(this)); }) function myFunc(thisObj) { thisObj.parent().parent().children().each(function() { alert(

Recursive constructor invocation error can't find solution

扶醉桌前 提交于 2019-12-02 14:24:02
I get the recursive construct overflow invocation error at the four public tuna parts (parts=maybe a class or something else?). It worked on the tutorial but not for me and can't seem to see where public class tuna { private int hour; private int minute; private int second; public tuna() { this(0,0,0); //default } public tuna(int h){ this(h,0,0); //with hours input } public tuna(int h, int m){ this(h,m,0); //with hours and minutes } public tuna(int h, int m, int s){ this(h,m,s); //with hours, minutes and seconds } You're doing a recursive call here: public tuna(int h, int m, int s){ this(h,m,s

Way to increment objects inside an array for 2 hit detection c+

丶灬走出姿态 提交于 2019-12-02 13:41:50
Im creating a basic game using SDL/C++. I need a way to implement 2 hit detection. When just trying one hit it works fine. Here is what i have for the two hit detection: int maxHit = 2; int hitCount = 0; // Detect collisions for(auto p : projectiles) { for(auto a : aliens) { if(p->CollidesWith(a) && hitCount == maxHit) { p->HandleCollision(); a->HandleCollision(); } if(p->CollidesWith(a) && hitCount != maxHit) { ++hitCount; } } } For some reason it works on a select few of the enemies on the screen and doesn't for others. EDITED TO MAKE IT CLEARER Yes, this identifies the object on which the