this

Javascript: why “this” inside the private function refers to the global scope?

寵の児 提交于 2019-11-28 07:34:28
Consider the following code: function A() {} A.prototype.go = function() { console.log(this); //A { go=function()} var f = function() { console.log(this); //Window }; f(); } var a = new A(); a.go(); Why does 'this' inside function 'f' refers to the global scope? Why it is not the scope of function 'A' ? JavaScript has a different concept of what the special name this refers to than most other programming languages do. There are exactly five different ways in which the value of this can be bound in the language. The Global Scope this; When using this in global scope, it will simply refer to the

Using “this” with methods (in Java)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 07:00:05
what about using "this" with methods in Java? Is it optional or there are situations when one needs to use it obligatory? The only situation I have encountered is when in the class you invoke a method within a method. But it is optional. Here is a silly example just to show what I mean: public class Test { String s; private String hey() { return s; } public String getS(){ String sm = this.hey(); // here I could just write hey(); without this return sm; } } Three obvious situations where you need it: Calling another constructor in the same class as the first part of your constructor

JavaScript | Angular | Controller As Syntax: Cannot Use `this`

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:34:04
问题 Cannot Use ControllerAs this Can anyone explain the following scenario to me, please? Works ng-controller="Parent as thus" Breaks ng-controller="Parent as this" That single letter which makes it a keyword -- which I want -- wrecks the forest. Why is this? P.S. I'm aware of the vm convention, but I find it disturbs portability of controllers/viewmodels. 回答1: The problem is certainly not that this is a reserved word in JavaScript. There is no rule in the controller as syntax that says you would

How does Function.bind.bind(Function.call) uncurry?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 06:33:12
We have this line in my code base: var uncurryThis = Function.bind.bind(Function.call); That I'm trying to work through. Presumably, it uncurries. How do I work this out? I guess it's a version of Function.bind whose own this is bound to Function.call . Doesn't help me enough. And I haven't found any uses, so I'm not even sure if you call it standalone or need to call it "as a method", only, you know, bind it first. It passes the call function to the bind function, with the bind function itself being the value of this . Thus you get in return a wrapper around the bind function that arranges

Using this inside a static function fails

我们两清 提交于 2019-11-28 04:37:10
I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context. How can I get this to work? public static function userNameAvailibility() { $result = $this->getsomthin(); } This is the correct way public static function userNameAvailibility() { $result = self::getsomthin(); } Use self:: instead of $this-> for static methods . See: PHP Static Methods Tutorial for more info :) You can't use $this inside a static function, because static functions are independent of any instantiated object. Try making the function not static. Edit : By

Google Closure bind / resolve issues with the this keyword

吃可爱长大的小学妹 提交于 2019-11-28 04:35:48
问题 What is the Google Closure 's solution for resolving the issues with the this keyword in JavaScript callback functions. It would be so useful in OO style programming. Is there any conventions or style for OOP in Google Closure ??? update How can I access this.darklayer in ViewportSizeMonitor handler??? goog.require('goog.dom'); goog.require('goog.events'); goog.require('goog.events.EventType'); goog.require('goog.math.Size'); goog.require('goog.style'); goog.require('goog.dom

React this is undefined

↘锁芯ラ 提交于 2019-11-28 04:13:12
问题 React this is undefined in promise . Here is my code: export default class Album extends React.Component { constructor(props) { super(props); } componentDidMount () { console.log(this.props.route.appState.tracks); // `this` is working axios({ method: 'get', url: '/api/album/' + this.props.params.id + '/' + 'tracks/', headers: { 'Authorization': 'JWT ' + sessionStorage.getItem('token') } }).then(function (response) { console.log(response.data); this.props.route.appState.tracks.concat(response

Returning *this in member functions

泄露秘密 提交于 2019-11-28 02:50:19
问题 I recently used a library that allows the following type of syntax: MyClass myObject; myObject .setMember1("string value") .setMember2(4.0f) .setMember3(-1); Obviously this is accomplished by having the setters return a MyClass & type; something like return *this. I like the way this code looks, but I don't see it a lot. When that happens I'm usually suspicious as to why. So, is this a bad practice? What are some of the implications of doing it like this? 回答1: This is sometimes referred to as

Where's the difference between self and $this-> in a PHP class or PHP method?

我只是一个虾纸丫 提交于 2019-11-28 02:41:38
问题 Where's the difference between self and $this-> in a PHP class or PHP method? Example: I've seen this code recently. public static function getInstance() { if (!self::$instance) { self::$instance = new PDO("mysql:host='localhost';dbname='animals'", 'username', 'password');; self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } return self::$instance; } But I remember that $this-> refers to the current instance (object) of a class (might also be wrong). However, what's

Does every c++ member function take `this` as an input implicitly?

≡放荡痞女 提交于 2019-11-28 02:29:14
问题 When we create a member function for a class in c++, it has an implicit extra argument that is a pointer to the calling object -- referred as this . Is this true for any function, even if it does not use this pointer. For example, given the class class foo { private: int bar; public: int get_one() { return 1; // Not using `this` } int get_bar() { return this->bar; // Using `this` } } Would both the functions ( get_one and get_bar ) take this as an implicit parameter, even though only one of