Referencing a record (anonymous object) from within itself

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Referencing a record (anonymous object) from within itself

Jordo Odroj
In js you can do:

var x = {
   myName: "joe",
   sayName: function() {alert(this.myName);}
};

x.sayName();

I found this to be very helpful in creating small code.

However, the 'this' keyword in javascript means something *totally*
different in Haxe. I like the way haxe uses the *this* keyword.
However, having a record be able to reference itself is extremely
helpful. This avoids the need to create a class, when all you want is
a small little package of functionality.

If somehow *x* above could be in the scope of any closure created in
the members, this would be extremely helpful.

Note: I do *not* want javascript notion of *this*. Javascript's "this"
is whichever object happened to be to the left of the dot when the
method was invoked. If you pick up the method and attach it to another
object, "this" is changed. What I'm wishing for here, is just for x to
be bound in the closure of any function members of x.

1. Does anyone agree/disagree?
2. Has anyone thought of a workaround that doesn't involve creating
another class definition?


Jordo

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Referencing a record (anonymous object) from within itself

Tony Polinelli
something similar is planned to be implemented with local functions.

using a similar pattern, i think this works:

var x;
x = {
  myName: "joe",
  sayName: function() {alert(x.myName);}
};

maybe use this if you currently need things to work. (i havent tested)




On Thu, Mar 24, 2011 at 6:54 PM, Jordo Odroj <[hidden email]> wrote:
In js you can do:

var x = {
  myName: "joe",
  sayName: function() {alert(this.myName);}
};

x.sayName();

I found this to be very helpful in creating small code.

However, the 'this' keyword in javascript means something *totally*
different in Haxe. I like the way haxe uses the *this* keyword.
However, having a record be able to reference itself is extremely
helpful. This avoids the need to create a class, when all you want is
a small little package of functionality.

If somehow *x* above could be in the scope of any closure created in
the members, this would be extremely helpful.

Note: I do *not* want javascript notion of *this*. Javascript's "this"
is whichever object happened to be to the left of the dot when the
method was invoked. If you pick up the method and attach it to another
object, "this" is changed. What I'm wishing for here, is just for x to
be bound in the closure of any function members of x.

1. Does anyone agree/disagree?
2. Has anyone thought of a workaround that doesn't involve creating
another class definition?


Jordo

--
haXe - an open source web programming language
http://haxe.org



--
Tony Polinelli
http://touchmypixel.com

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Referencing a record (anonymous object) from within itself

Franco Ponticelli
On Thu, Mar 24, 2011 at 12:27 PM, Tony Polinelli <[hidden email]> wrote:
something similar is planned to be implemented with local functions.

If you refer to this it already works:

static function hi() {
  function hello() return "say hi";
  return hello();

--
haXe - an open source web programming language
http://haxe.org