this

Why is the usage of $this in PHP necessary when referencing methods or variables in the same class?

谁说胖子不能爱 提交于 2019-12-01 12:23:20
I was explaining to a Java developer why his method call wasn't working. He just needed to add $this->method_name(); He then asked me, "Why do I need to add $this to the method when it's declared in the same class?" I didn't really know how to answer. Maybe it's because PHP has a global namespace and it you need to explicitly tell it that the method you are looking for belongs to the current class? But then why doesn't PHP check the current class for the method BEFORE looking at the global namespace? The problem would also be that if you declared a function foo() and a method foo() , php would

Callback this context [duplicate]

半世苍凉 提交于 2019-12-01 12:08:04
This question already has an answer here: How to access the correct `this` inside a callback? 10 answers in App: var bootstrap = new Bootstrap(); bootstrap.init( this, this.onBootstrapComplete ); in Bootstrap: this.init = function( app, completeHandler ){ _app = app; _completeHandler = completeHandler; ... } ... var _allReady = function(){ _completeHandler( _app ); } back in App: this.onBootstrapComplete = function( app ) { app.something(); app.someValue = ... } I wanted to get this context inside onBootstrapComplete. It works but it doesn't look right :) If let's say I wanted to call

Why is the usage of $this in PHP necessary when referencing methods or variables in the same class?

匆匆过客 提交于 2019-12-01 11:54:32
问题 I was explaining to a Java developer why his method call wasn't working. He just needed to add $this->method_name(); He then asked me, "Why do I need to add $this to the method when it's declared in the same class?" I didn't really know how to answer. Maybe it's because PHP has a global namespace and it you need to explicitly tell it that the method you are looking for belongs to the current class? But then why doesn't PHP check the current class for the method BEFORE looking at the global

Why does JavaScript “this” returns different values in Node and Browser environments?

做~自己de王妃 提交于 2019-12-01 11:50:02
After watching Kyle Simpson's Advanced JavaScript course on Pluralsight, I created a simple code snippet to try out this binding. I usually work in SublimeText editor and have node build engine configured in it and occasionally I run the same code in browser as well. I have noticed one difference in the program output when execute code in Node and in Browser [Chrome]. Following is the code snippet to try out this binding. function foo() { console.log(this.bar); } var bar = "bar1"; var obj = {bar : "bar2"}; foo(); foo.call(obj); When I execute it using Sublime, it returns undefined and bar2 .

“this” does not refer to what I want [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-01 11:42:41
This question already has an answer here: How to access the correct `this` inside a callback? 10 answers In one of my classes, a method performs AJAX requests. In the callback function of a request, I need to call another method of my object, using this . But this does not refer to my object in this context, so I don't know how to do... Is it only possible ? To clarify, please consider the following code : function MyClass(arg) { this.foo = arg; } MyClass.prototype = { myMethod: function() { console.log("I am myMethod"); }, myGet: function (){ $.get("http://example.iana.org/",function(data){

Groovy instance.metaclass vs this.metaclass

风流意气都作罢 提交于 2019-12-01 11:35:15
问题 I have a following script: task myTask {} class Person { Person() { Person instance = this println this.metaClass.class.name println this.getMetaClass().class.name println instance.metaClass.class.name println instance.getMetaClass().class.name } } Person person = new Person() And the output is : groovy.lang.MetaClassImpl groovy.lang.MetaClassImpl org.codehaus.groovy.runtime.HandleMetaClass org.codehaus.groovy.runtime.HandleMetaClass Can anyone explain to me what is going on? Thanks in

“this” does not refer to what I want [duplicate]

巧了我就是萌 提交于 2019-12-01 11:33:45
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 5 years ago . In one of my classes, a method performs AJAX requests. In the callback function of a request, I need to call another method of my object, using this . But this does not refer to my object in this context, so I don't know how to do... Is it only possible ? To clarify, please consider the following code : function MyClass(arg) { this.foo = arg; } MyClass.prototype = {

How do I refer to actual 'this' inside CoffeeScript fat-arrow callback?

早过忘川 提交于 2019-12-01 11:26:50
The title says it all. When I use the fat-arrow in CoffeeScript, it stores this first before calling the function. For example: class myClass constructor: -> element = $ "#id" element.click -> @myMethod(@value) return return myMethod: (c)-> window.console.log(c) return would yield var myClass; myClass = (function() { function myClass() { var element; element = $("#id"); element.click(function() { this.myMethod(this.value); }); return; } myClass.prototype.myMethod = function(c) { window.console.log(c); }; return myClass; })(); Now on line#8 of JavaScript, this.myMethod is wrong. In this scope,

How to avoid “this” refering to the DOM element, and refer to the object

99封情书 提交于 2019-12-01 11:20:18
问题 I have a problem that I can't work around. The context is: I want to have an inheritance chain, and a method of objects that belong to this inheritance has to be a event handler, and at the same time be able to reach the object properties. I am giving a try to writing JavaScript without the "new" word, and using instead Object.create() with some inheritance hierarchy. So first this approach. So I have a blueprint for the rest of my objects (myProto), and then I create objects with Object

Using selectors and $(this) in Jquery Ajax response

牧云@^-^@ 提交于 2019-12-01 11:17:17
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 hyperlink with a specific rel attribute function(msg){ var container = $(this).parent().attr('id'); alert