this

Access to static properties via this.constructor in typescript

ぃ、小莉子 提交于 2019-12-03 22:31:48
I want to write es6 class: class SomeClass { static prop = 123 method() { } } How to get access to static prop from method() without use SomeClass explicitly? In es6 it can be done with this.constructor , but in typescript this.constructor.prop causes error " TS2339: Property 'prop' does not exist on type 'Function' ". but in typescript this.constructor.prop causes error "TS2339: Property 'prop' does not exist on type 'Function'". Typescript does not infer the type of constructor to be anything beyond Function (after all ... the constructor might be a sub class). So use an assertion: class

Call static method from instance in PHP, future deprecation?

情到浓时终转凉″ 提交于 2019-12-03 22:21:45
While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example: class MyExample{ private static $_data = array(); public static function setData($key, $value){ self::$_data[$key] = $value; } // other non-static methods, using self::$_data } // to decouple, another class or something has been passed an instance of MyExample // rather than calling MyExample::setData() explicitly // however, this data is now

What do these “this” mean in this JavaScript code?

独自空忆成欢 提交于 2019-12-03 22:00:35
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>test</title> </head> <body> <script type="text/javascript" charset="utf-8"> (function(){ // this var test=function(){ //this return function(){ //this }; } (function(){ //this var a={ p1:function(){ //this } }; })(); })(); </script> </body> </html> David Dorward already mentioned about JavaScript: The Good Parts by Douglas Crockford. From Section 4.3 of that excellent book: Invoking a function

Why does Backbone.js model's 'on()' take 'this' as last parameter if it's almost always going to be this?

好久不见. 提交于 2019-12-03 20:12:09
I'm just getting into Backbone, and one thing that I don't understand is why the 'on()' method for models always takes three arguments--event, handler, and context. It seems that almost always 'this' is used for context and I haven't seen any other usage. Even if there were, since I haven't seen one yet it must be pretty rare. So my question is: When does one use a context other than 'this', and why is Backbone designed this way? By the way, I do understand why you need to provide context, it's just that I wonder why the method syntax specifies that I use three arguments instead of making the

How to function call using 'this' inside forEach loop

。_饼干妹妹 提交于 2019-12-03 18:45:42
问题 In the following object, I have a problem using the 'this' reference: function SampleObject(){ this.addObject = function(object){...} ... // more code here ... this.addNewObjects= function(arr){ arr.forEach( function (obj) { this.addObject(new Obj(obj.prop1, obj.prop2)); }); } } I'm assuming the context is changing and that 'this' refers the iterated 'obj', and not 'SampleObject'. I've solved the problem using a normal for loop however, i'm curuois to why this is not working, and would like

Javascript `this` in objects-in-objects?

独自空忆成欢 提交于 2019-12-03 17:11:50
Sorry for fuzzy post title, I can't formulate the correct name in English for this post. For example I have such an object: var APP = { a : 1, b : function(){ return this.a; } } In such way, if I call console.log ( APP.b() ) than this will be referring to APP and result will be 1 . But how to connect to APP from sub-object? Example: var APP = { a : 1, b : function(){ return this.a; }, c : { c1: function(){ return APP.a; } } } console.log ( APP.c.c1() ) // 1 This example works, but it's bad idea to point directly to APP. For example: APP2 = APP; APP = null; console.log ( APP2.b() ); // 1

jQuery using 'this' in an if statement

浪子不回头ぞ 提交于 2019-12-03 16:59:09
问题 I'm using an if statement in order to determine if an element has any children. If it does NOT have any children, I want to do something to that element only. Here's the premise of what I'm trying to do: if ($("#div a").children().length > 0){ $(this).hide(); } So if an <a> tag has no children, I want to do something to that specific element (or multiple elements that also have no children). The problem is that this hasn't been defined because it's an if statement. I could be completely

d3.select(this) works on mouseover, but not on function called in mouseover

感情迁移 提交于 2019-12-03 12:59:11
I am new to javascript and currently struggling with selecting the this object while trying to do a d3 selection. I've made the following example, with a function I'm calling, and an on mousemove event: function changeFont() { d3.select(this) .attr('font-size', '2em') } ... .on('mousemove', function() { var mouse = d3.mouse(this); var xVal = mouse[0]; // this would work, but not when its called in a function // d3.select(this) // .attr('font-size', '2em') // this works d3.select(this) .attr("opacity", 1) // this doesnt changeFont() }); In my main script not shown here, I am organizing my code

What is a best practice for ensuring “this” context in Javascript?

ぃ、小莉子 提交于 2019-12-03 12:38:18
Here's a sample of a simple Javascript class with a public and private method (fiddle: http://jsfiddle.net/gY4mh/ ). function Example() { function privateFunction() { // "this" is window when called. console.log(this); } this.publicFunction = function() { privateFunction(); } } ex = new Example; ex.publicFunction(); Calling the private function from the public one results in "this" being the window object. How should I ensure my private methods are called with the class context and not window? Would this be undesirable? Using closure. Basically any variable declared in function, remains

what does this() mean in Java [duplicate]

我的未来我决定 提交于 2019-12-03 12:34:16
This question already has an answer here: Whats the use of this() in linkedlist.java 2 answers what does this() mean in Java? It looks it is only valid when put this(); in the class variable area. Any one has idea about this? Thanks. Peter Lawrey It means you are calling the default constructor from another constructor. It has to be the first statement and you cannot use super() if you have. It is fairly rare to see it used. It's a call to the no-argument constructor, which you can call as the first statement in another constructor to avoid duplicating code. public class Test { public Test() {