Class Fonctions

Get help with using AutoHotkey and its commands and hotkeys
hasantr
Posts: 155
Joined: 05 Apr 2016, 14:18

Class Fonctions

11 Jul 2019, 05:13

What is the purpose of the _ and __ signs used in at the beginning of functions in classes? I really have a hard time understanding.
Helgef
Posts: 3777
Joined: 17 Jul 2016, 01:02
Contact:

Re: Class Fonctions

11 Jul 2019, 06:04

They have no special meaning to the program. A few reserved method names start with __, these methods have special meaning but not due the __. See meta functions. Also see _newenum .

Sometimes script authors prefix underscores to indicate that a method is intended to be private, it is only a naming convention and has no real impact.

Cheers.
hasantr
Posts: 155
Joined: 05 Apr 2016, 14:18

Re: Class Fonctions

12 Jul 2019, 01:30

Thank you so much. That was certainly enough explanation for me.

Code: Select all

this 
and

Code: Select all

base
: Can you explain the statements? I have difficulty understanding the guide.
Helgef
Posts: 3777
Joined: 17 Jul 2016, 01:02
Contact:

Re: Class Fonctions

12 Jul 2019, 01:48

When you define a method, autohotkey will add one extra parameter to it, called this, example, if you write,

Code: Select all

class myClass {
	myMethod(Param1, Param2) {
	}
}
then, when the script is loaded, AHK will turn it into

Code: Select all

class myClass {
	myMethod(this, Param1, Param2) {
	}
}
Then, when you call the method, autohotkey will automatically pass the invoking object to the this parameter. For example, if you do,

Code: Select all

myObj := new myClass
myObj.myMethod(1,2)
then AHK will pass myObj to myMethod as the first parameter, before the parameters you passed yourself, i.e., 1,2 in this example, so inside myMethod the this parameter is now equal to myObj. But if you call the method with myObj2, this is equal to that object instead.
The keyword base is typically used to call an overridden method. Example,

Code: Select all

c := new A
c.myMethod() ; calls (1)

class A extends B {
	myMethod() {			; (1)
		base.myMethod()		; calls (2)
	}
}
class B {
	myMethod() {			; (2) 
	}
}
Cheers.
swagfag
Posts: 2610
Joined: 11 Jan 2017, 17:59

Re: Class Fonctions

12 Jul 2019, 07:14

_NewEnum is a single underscore
:think:
hasantr
Posts: 155
Joined: 05 Apr 2016, 14:18

Re: Class Fonctions

Today, 02:36

Helgef wrote:
12 Jul 2019, 01:48
When you define a method, autohotkey will add one extra parameter to it, called this, example, if you write,

Code: Select all

class myClass {
	myMethod(Param1, Param2) {
	}
}
then, when the script is loaded, AHK will turn it into

Code: Select all

class myClass {
	myMethod(this, Param1, Param2) {
	}
}
Then, when you call the method, autohotkey will automatically pass the invoking object to the this parameter. For example, if you do,

Code: Select all

myObj := new myClass
myObj.myMethod(1,2)
then AHK will pass myObj to myMethod as the first parameter, before the parameters you passed yourself, i.e., 1,2 in this example, so inside myMethod the this parameter is now equal to myObj. But if you call the method with myObj2, this is equal to that object instead.
The keyword base is typically used to call an overridden method. Example,

Code: Select all

c := new A
c.myMethod() ; calls (1)

class A extends B {
	myMethod() {			; (1)
		base.myMethod()		; calls (2)
	}
}
class B {
	myMethod() {			; (2) 
	}
}
Cheers.
Thank you so much. But I could be an idiot. Because I still don't understand. :(
Am I not enough to learn object orientation? I don't know where the problem is. But maybe a more obvious example would be my remedy. Even if I don't want to bother you.
Helgef
Posts: 3777
Joined: 17 Jul 2016, 01:02
Contact:

Re: Class Fonctions

Today, 04:02

Perhaps you will have more luck with one of the :arrow: tutorials. I don't think I can help you unless you make a more specific question, but I think you should start a new thread for that anyways.

@swagfag, the op specifically asked about single and double underscore prefixed method names, hence why I mentioned _newenum ;) .

Cheers.
hasantr
Posts: 155
Joined: 05 Apr 2016, 14:18

Re: Class Fonctions

Today, 04:12

Helgef wrote:
Today, 04:02
Perhaps you will have more luck with one of the :arrow: tutorials. I don't think I can help you unless you make a more specific question, but I think you should start a new thread for that anyways.

@swagfag, the op specifically asked about single and double underscore prefixed method names, hence why I mentioned _newenum ;) .

Cheers.
:thumbup: Thank. Somehow I have to do this myself. Thank you for trying. :)
I'm constantly reviewing case studies, Autohotkey will take some time, but don't give up. :)

Return to “Ask For Help”

Who is online

Users browsing this forum: Flipeador, Google [Bot], WilburBr, Wontyd and 106 guests