this

Using '$(this)' In A Function

和自甴很熟 提交于 2019-11-26 23:34:54
问题 I am creating a menu that opens and closes using jQuery. In simple terms, it works like this: function open_menu() { $(this).next('ul.sub-menu').css('display', 'block').stop(true, false).animate({ width: '235px', }, 500); } function close_menu() { // close code here } status = 'closed'; // set the default menu status $('a').click(function() { switch(status) { case 'closed': open_menu(); break; case 'open': close_menu(); break; } } If I take the contents of open_menu() and put it in place of

Javascript this points to Window object

不打扰是莪最后的温柔 提交于 2019-11-26 23:17:26
问题 I have the following code. I expected to see "archive" object on my firebug console, but I see Window object. Is it normal? var archive = function(){} archive.prototype.action = { test: function(callback){ callback(); }, test2: function(){ console.log(this); } } var oArchive = new archive(); oArchive.action.test(oArchive.action.test2); 回答1: oArchive.action.test2 gets you a reference to a function that callback then points to, but that function is then called using callback() , which means it

JavaScript this refers to window instead of object inside function

不羁岁月 提交于 2019-11-26 23:16:17
问题 I get confused on a JavaScript this reference situation. I am working on a code that I declare function inside an object method. (The reason is to tidy up code inside an object method, while keeping the functions private to the method.) The following is an experiment to re-produce my problem. I found that the this inside greeting function refers to the window scope instead of person scope. var person = { nickname: "Makzan", sayHi: function() { console.log(this); var greeting = function() {

IIFE context issues

限于喜欢 提交于 2019-11-26 23:15:59
问题 In the following construct: (function(){ var x = function(){ alert('hi!'); } var y = function(){ alert("hi again!"); } this.show = function(){ alert("This is show function!"); } })(); Why does this refer to window object? Should everything inside IIFE be isolated from global scope? Are x and y functions also properties of window global object? Also, even if I use put var h = ... at the beginning: var h = (function(){ var x = function(){ alert('hi!'); } var y = function(){ alert("hi again!");

What is an “incompletely constructed object”?

十年热恋 提交于 2019-11-26 22:32:27
Goetz's Java Concurrency in Practice , page 41, mentions how this reference can escape during construction. A "don't do this" example: public class ThisEscape { public ThisEscape(EventSource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); } } Here this is "escaping" via the fact that doSomething(e) refers to the enclosing ThisEscape instance. The situation can be fixed by using static factory methods (first construct the plain object, then register the listener) instead of public constructors (doing all the work). The book goes on:

React 'this' is undefined in Chrome developer tools [duplicate]

泪湿孤枕 提交于 2019-11-26 21:57:01
问题 This question already has an answer here : Value of “this” is incorrect when debugging Babel transpiled React with Chrome Devtools (1 answer) Closed 3 months ago . When I inspect my React code in the Chrome developer tools 'Source' tab, and I hover over the 'this.props' or even 'this' keyword / add it to watches, it shows up as undefined. Even though the code referred to executes successfully. Very annoying.... is this is bug? Is there a workaround for this? 回答1: Due to how lexical this is

How to explain 'this' keyword in a best and simple way?

时间秒杀一切 提交于 2019-11-26 21:53:58
问题 I am using 'this' keyword for a long time. But when someone asks me to explain it, I am confused that how to explain it. I know that I can use this in a method of class to access any variable and method of the same class. class MyClass{ function MyMethod1(){ echo "Hello World"; } function MyMethod2(){ $this->MyMethod1(); } } Is it a object of a class that we don't need to initialise and can be used only within the class or anything else. How to explain? Thanks 回答1: A class is a mold for an

How to refer to enclosing instance from C++ inner class?

人走茶凉 提交于 2019-11-26 21:35:50
问题 In C++, an object refers to itself via this . But how does an instance of an inner class refer to the instance of its enclosing class? class Zoo { class Bear { void runAway() { EscapeService::helpEscapeFrom ( this, /* the Bear */ ??? /* I need a pointer to the Bear's Zoo here */); } }; }; EDIT My understanding of how non-static inner classes work is that Bear can access the members of its Zoo , therefore it has an implicit pointer to Zoo . I don't want to access the members in this case; I'm

Writing a Kotlin util function which provides self-reference in initializer

☆樱花仙子☆ 提交于 2019-11-26 21:35:35
问题 I'm trying to generalize my hack from an answer to another question. It should provide a way to reference a value which is not constructed yet inside its initializer (of course, not directly, but in lambdas and object expressions). What I have at the moment: class SelfReference<T>(val initializer: SelfReference<T>.() -> T) { val self: T by lazy { inner ?: throw IllegalStateException("Do not use `self` until initialized.") } private val inner = initializer() } fun <T> selfReference(initializer

What does $this mean in PHP? [duplicate]

怎甘沉沦 提交于 2019-11-26 21:34:54
问题 Possible Duplicate: PHP: self vs this Hello, Could you help me understanding the meaning of the PHP variable name $this ? Thank you for your help. 回答1: $this refers to the class you are in. For example Class Car { function test() { return "Test function called"; } function another_test() { echo $this->test(); // This will echo "Test function called"; } } Hope this helps. 回答2: You might want to have a look at the answers in In PHP5, what is the difference between using self and $this? When is