this

“Invalid use of 'this' in non-member function” in objective-c context?

拜拜、爱过 提交于 2019-11-28 02:01:17
问题 Using Xcode. In this code (func is declared in interface), tells subj error, standing on string with 'self'. + (void) run: (Action) action after: (int) seconds { [self run:action after:seconds repeat:NO]; } What the... ? 回答1: self is an instance variable used to refer to an instance of the current object. You are attempting to use it in a class level method +(void)... where self has no meaning. Try using a shared instance, or passing an instance of the class in question to the method. + (void

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

佐手、 提交于 2019-11-28 01:21:16
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 A class is a mold for an object: it specifies how the object looks like (variables) and what it can do (functions). If you instanciate

Is it OK to use “delete this” to delete the current object?

元气小坏坏 提交于 2019-11-28 01:09:05
I'm writing a linked list and I want a struct's destructor (a Node struct) to simply delete itself, and not have any side effects. I want my list's destructor to iteratively call the Node destructor on itself (storing the next node temporarily), like this: //my list class has first and last pointers //and my nodes each have a pointer to the previous and next //node DoublyLinkedList::~DoublyLinkedList { Node *temp = first(); while (temp->next() != NULL) { delete temp; temp = temp->next(); } } So this would be my Node destructor: Node::~Node { delete this; } Is this acceptable, especially in

Javascript “this” reference for onclick event not working

若如初见. 提交于 2019-11-28 01:04:20
I'm trying to call a function with the "onclick" event as so: <td id="A1" onclick="move()" class="white"></td> <td id="A2" onclick="move()" class="painted bp"></td> <td id="A3" onclick="move()" class="white"></td> In the function itself, i refer to "this": function move(e){ var myId = this.id; alert("myId"); } When I run the whole thing, the alert says 'undefined'. When I try alert(this) I get [object window]. I'm working with IE9, btw. Thanks Shadow Wizard When calling a function from an event handler, its this isn't set by the handler (though you can pass this from the handler per Xdazz's

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

杀马特。学长 韩版系。学妹 提交于 2019-11-27 23:49:51
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: SelfReference<T>.() -> T): T { return SelfReference(initializer).self } It works, see this example:

ES6 arrow function lexical this in V8

大兔子大兔子 提交于 2019-11-27 23:38:16
I have the following ES6 code using a fat arrow function: var test = { firstname: 'David', fn: function() { return ['one', 'two', 'tree'].map(() => this.firstname) } } console.log(test.fn()) According to how arrow functions are supposed to work I'd expect this to be the test object. ES6Fiddle , Traceur and Firefox produce the expected output which is ["David", "David", "David"] . When enabling those features in Chrome using chrome://flags/#enable-javascript-harmony , however, I get [undefined, undefined, undefined] . If you console.log(this) it shows that it is the window object and you get an

Difference between this and base [closed]

隐身守侯 提交于 2019-11-27 23:16:15
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I am interested to know the difference between this and base object in C# . What is the best practice when using them? 回答1: this represents the current class instance while base the parent. Example of usage:

JavaScript this refers to window instead of object inside function

倾然丶 夕夏残阳落幕 提交于 2019-11-27 23:12:05
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() { console.log(this); return "Aloha " + this.nickname; } console.log(greeting()); } } person.sayHi(); (same

IIFE context issues

廉价感情. 提交于 2019-11-27 23:09:05
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!"); } this.show = function(){ alert("This is show function!"); } })(); this still refers to window object -

Javascript this points to Window object

一曲冷凌霜 提交于 2019-11-27 23:08:06
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); 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 is not called as a method and hence this is the global object. The key point is that this is not bound to a