this

java “this” keyword proper use

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 11:16:53
I have a Fraction class using keyword this in my constructor: public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; adjustSigns(); if(this.denominator == 0 ) { throw new FractionException(" Undefined Fraction "); } } I also have a method : public FractionInterface multiply(FractionInterface secondFraction) { Fraction second = (Fraction) secondFraction; Fraction answer = new Fraction ((numerator * second.numerator), (denominator * second.denominator)); answer.reduceToLowestTerms(); return answer; } The above method works fine when I

how do i get “this = this” in prototype working

依然范特西╮ 提交于 2019-12-01 11:10:01
问题 Ok peep's so I know it's bad practice to mess with prototypes but here it is anyway... Array.prototype.rev= function(){ this.reverse(); } Works fine! Updates the source array variable, ary , as expected eg: ary = [123, 456]; ary.rev(); // result: ary == [456, 123] My problem comes when writing a similar property for String . What I would like to do is something like this... String.prototype.rev= function(){ this.split(''); this.reverse(); this.join(''); } Seems simple enough right! Split the

Javascript object literal, how to solve context?

大兔子大兔子 提交于 2019-12-01 09:31:46
I would like to start organizing my code properly, so I want to use object literals. In the following case, I'm doing a pseudo class. I would like that init() could work as a constructor, but unfortunately, I'm not seeing how to set attributes based on object context. var car = { context : this, wheels : 0, color : '', speed : 0, init : (function(x){ console.log(x); x.wheels = 4; x.color = 'red'; x.speed = 120; })(context) }; console.log(car.color); You can't immediately run a function like that whilst declaring an object literal. What you can do: var car = { init : function(wheels,color,speed

Using selectors and $(this) in Jquery Ajax response

邮差的信 提交于 2019-12-01 09:09:28
问题 I am wondering how I use Jquery selectors within the Ajax response. My site has a feed and each main block has a unique ID, but I dont want to uniquly ID every div thats within that (which is alot). So far $(this) returns the clicked ID from within the main event handler, but when I use it within the response function, I get 'undefined'. How can I achieve the same effect as $(this) from within the response or do I have to find a unique ID somewhere? The main function is being called via a

why the code this point to window object?

孤街浪徒 提交于 2019-12-01 06:49:39
my code is: var length = 20; function fn(){ console.log(this.length); } var o = { length:10, e:function (fn){ fn(); arguments[0](); } } o.e(fn); the output is 20,1 ,who can tell me why? When the this keyword occurs inside a function, its value depends on how the function is called . In your case, fn() is called without providing the a this value, so the default value is window . With arguments[0]() , the context is the arguments object, whose length is 1 . The point is it does not matter where the function is called , but it matters how the function is called . var length = 20; function fn(){

Best approach to avoid javascript's “this” mistakes

佐手、 提交于 2019-12-01 06:38:58
In some cases, the this keyword may not refer to the object I expect it to. (recent example: in an key event, in my XBL) What's the best approach to avoid this kind of mistake? For now, I'm using always the $.fn from jQuery to store my variables, but I'm not sure if it's the best approach. Don't avoid using this . Just use it the right way. Javascript is a prototype based object oriented language. If you create your objects using the object prototype should always know what this refers to. jQuery.fn is the same thing as jQuery.prototype . jQuery.fn is just an alias. You can also check the this

Can I pass “this” as a parameter to another function in javascript

馋奶兔 提交于 2019-12-01 05:49:34
I have this: $('#slider li').click(function () { var stepClicked = $(this).index(); alert(stepClicked); if (stepClicked != 0) { $('#cs_previous').removeClass('cs_hideMe'); } else { $('#cs_previous').addClass('cs_hideMe'); } $('li.cs_current').removeClass('cs_current'); $($(this)).addClass('cs_current'); moveToNextImage(stepClicked); function moveToNextImage(stepClicked) { alert(stepClicked); var currentIs = $('li.cs_current').index(); var newLeftEdge = currentIs - stepClicked; $('.cs_riskStageImage').fadeTo(200, .2).animate({ left: newLeftEdge }, "fast").fadeTo(200, 1); }; }); the alert shows

jQuery - 'this' selector doesn't work inside callback function [duplicate]

会有一股神秘感。 提交于 2019-12-01 05:39:47
Possible Duplicate: $(this) doesn't work in a function I'm writing post removing code in jQuery, The removing itself is made via post-request to backeds, after server returns 200, I want to delete this post on client-side. $('.delete-post').click(function() { $.post($(this).attr('href'), {}, function(data) { $(this).closest('.post').remove(); }); return false; }); However, I've noticed that inside function(data) {...) selector 'this' doesn't work. I need to remove closest to $('.delete-post') div with class '.post'. How to manage this problem? Thanks! Adil $(this) exists in the click event but

Is there a “this” keyword in Ada?

女生的网名这么多〃 提交于 2019-12-01 05:37:28
问题 Specifically, is there a way for a task to get a reference to itself? For example: task type someTask; type someTaskAccessor is access someTask; task body someTask is pointerToTask : someTaskAccessor; begin pointerToTask = this; end someTask; 回答1: the most evident solution i could suggest is to declare a rendez-vous (an entry) at the very beginning of your task, to which you pass a reference to the task just created. the other possibility is using a discriminant to your task type, whose role

How to bind methods when destructuring an object in JavaScript?

左心房为你撑大大i 提交于 2019-12-01 05:00:57
How to bind methods when destructuring an object in JavaScript? const person = { getName: function() { console.log(this); } }; var a = person.getName; var b = person.getName.bind(person); var {getName: c} = person; person.getName(); //=> {getName: [Function]} a(); //=> window or global b(); //=> {getName: [Function]} c(); //=> window or global I want c to log in the console its "parent" object {getName: [Function]} . Is there any way to bind all methods when destructuring an object in one destructuring line? No, there is no way. Functions detached from objects lose the original context. And