Object Method

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Object Method

18 Mar 2020, 06:03

Is there a way to define method in short?
Classic:

Code: Select all

class proto {
	w1() => MsgBox('Way 1') 
}
proto.prototype.DefineMethod('w2', (*)=>MsgBox('Way 2'))
pr:= proto.new()
pr.w1()
pr.w2()
With DefineMethod():

Code: Select all

pr:= {}
pr.DefineMethod('w2', (*)=>MsgBox('Way 2'))
pr.w2()
Needed:

Code: Select all

pr:= {w3: (*)=>MsgBox('Way 3')}
pr.w3() ;.call() needed
So is there way to define method in shorthand?
AHKv2.0 alpha forever.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Object Method

18 Mar 2020, 06:27

You need to make a function for it,

Code: Select all

f(list){
	local 
	o := {}
	for name, fn in list.ownprops()
		o.definemethod name, fn
	return o
}
; example,
pr := f({w3: (*)=>MsgBox('Way 3'), w4: (*) => msgbox('Way 4')})
pr.w3() ;.call() needed
pr.w4() ;.call() needed

_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: Object Method

19 Mar 2020, 14:34

Helgef wrote:
18 Mar 2020, 06:27
If I understand right: there no so way. BUT:

Code: Select all

DefineClass(ByRef clas, what) { ;inspired by Helgef --------------------------
	for name, meth in what.OwnProps()
		clas.DefineMethod(name, meth)
	return clas	
} ;----------------------------------------------------------------------------
;class clas {
;	p1:= 'prop 1'
;	p2:= 'prop 2'
;	m1() => 'meth 1'
;	m2() => 'meth 2'
;}
;pr:= clas.new()
pr:= DefineClass({p1: 'prop 1', p2: 'prop 2'}, {m1: (*)=>'meth 1', m2: (*)=>'meth 2'})
AHKv2.0 alpha forever.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: balawi28, Draken and 63 guests