this

javascript “this” keyword not referring to correct thing

一笑奈何 提交于 2019-12-04 05:14:42
问题 var ball = { x: 20, y: 500, vx: 100, vy: 100, width: 13, height: 13, draw: function() { var img = new Image(); img.src = 'images/ball.png'; img.onload = function(){ ctx.drawImage(img, this.x, this.y); }; }, I want the drawImage() line of code to refer to the ball.x and ball.y. Instead of using ball.x and ball.y, I want to use "this" keyword so that i can turn the ball object into a function that is a mass constructor/prototype if i end up wanting to (able to make ball1, ball2, ball3 etc.). I

Toggle next element with jQuery

核能气质少年 提交于 2019-12-04 04:44:13
问题 I have a problem with the this element (I know how this is working). I have a lot of that html structure. When I click on the a button, the div with class extra-options must be shown. But since I have a lot of the same html structure repeated throughout, when I click on the button, all the other div options are also shown. How can I fix it? Here’s the JavaScript: var buttonSubmit = $(".extra-options .details a.button"); buttonSubmit.click(function (e) { e.preventDefault(); var extraOptions =

How do I get the clicked element with inline HTML onclick and jQuery?

不羁的心 提交于 2019-12-04 04:29:37
问题 I'm creating a tags with this code: $('#td' + id).append('<p><a href="#" onclick="excluirArquivo(\'' + response + '\'); return false;"><img src="/erp/proposta/media/images/delete.png" alt="Excluir arquivo" /></a> ' + file + '</p>'); excluirArquivo function function excluirArquivo(arquivo) { $.ajax({ type: 'POST', url: '/erp/proposta/index.php/arquivo/remover/' + arquivo }); alert($(this)); } But this element inside excluirArquivo function is returning the Window object. How do I get the

What's the difference between this.bla to Object.prototype.bla

白昼怎懂夜的黑 提交于 2019-12-04 04:12:38
问题 Let's say I have this code: (function(global) { function Bar(foo) { this.foo = foo; return this; } Bar.prototype.getFoo = function() { return this.foo; }; Bar.prototype.setFoo = function(val) { return (this.foo = val); }; })(this); What is the difference between creating functions like setFoo with prototype and just doing it like this: function Bar(foo) { this.getFoo = function() { return this.foo; }; } I know what prototype is and what it means, I just can't figure out, why some people

Javascript function objects, this keyword points to wrong object

风流意气都作罢 提交于 2019-12-04 04:11:32
问题 I've got a problem concerning the javascript "this" keyword when used within a javascript functional object. I want to be able to create an object for handling a Modal popup (JQuery UI Dialog). The object is called CreateItemModal. Which i want to be able to instantiate and pass some config settings. One of the config settings. When the show method is called, the dialog will be shown, but the cancel button is not functioning because the this refers to the DOM object instead of the

jquery “this” binding issue on event handler (equivalent of bindAsEventListener in prototype)

人盡茶涼 提交于 2019-12-04 03:46:29
In jquery an event hadler's binding is the event generating DOM element (this points to the dom element). In prototype to change the binding of an event handler one can use the bindAsEventListener function; How can I access both the instance and the DOM element from a event handler? Similar to How can I bind an event handler to an instance in JQuery? function Car(){ this.km = 0; $("#sprint").click(this.drive); //setup event handler } // event handler // in it I need to access both the clicked element // and the binding object (instance of car) Car.prototype.drive = function(){ this.km += 10; /

Why am I failing to capture the “this” pointer by a lambda?

六月ゝ 毕业季﹏ 提交于 2019-12-04 03:39:28
Consider the following code: class A { public: void foo() { auto functor = [this]() { A * a = this; auto functor = [a]() // The compiler won't accept "this" instead of "a" { a->bar(); }; }; } void bar() {} }; In VC2010, using this instead of a lead to compilation errors. Among others: 1>main.cpp(20): error C3480: '`anonymous-namespace'::<lambda0>::__this': a lambda capture variable must be from an enclosing function scope 1>main.cpp(22): error C3493: 'this' cannot be implicitly captured because no default capture mode has been specified Which I don't understand. Does it mean it doesn't know if

Javascript object literal, how to solve context?

家住魔仙堡 提交于 2019-12-04 01:55:45
问题 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); 回答1: You can't immediately run a function

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

跟風遠走 提交于 2019-12-03 23:46:40
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 one linked as possible duplicate, because it's not generic question about way to create such reference,

why is this.callParent(arguments); called at the beginning of the constructors in ExtJS?

一个人想着一个人 提交于 2019-12-03 23:23:12
I noticed that in a lot of the programs I have been modifying recently they always call the parent arguments of the current object. I know that this is needed but don't have a solid understanding as to why this is a common practice. Any wisdom out there for a junior level developer... I should know this. This is the mechanism that ExtJS uses to support class inheritance in constructors. Calling this.callParent(arguments) in a constructor calls the constructor of the immediate parent class that's being extended. I don't know which version is this question about. But as far as my code goes, in