[Javascript] __Index metamethod

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

[Javascript] __Index metamethod

Post by Nixola »

Does Javascript have metatables and/or metamethods, like Lua? If it doesn't, how can I achieve a rasult similar to __index?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: [Javascript] __Index metamethod

Post by Roland_Yonaba »

Hi,
Well I do not think Javascript have an __index feature.
Though, the closest feature, IMHO, is the prototype feature.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: [Javascript] __Index metamethod

Post by Inny »

The answer is it depends on what you're using it for.

If it's to simulate a class inheritance scheme, you use the function.prototype.member syntax and copying between objects.

If it's to simulate python's getattr/setattr, then no, Javascript doesn't have that kind of feature yet but the next ECMA standard is supposed to have one (get and set functions).
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: [Javascript] __Index metamethod

Post by Nixola »

Code: Select all

If it's to simulate python's getattr/setattr, then no, Javascript doesn't have that kind of feature yet but the next ECMA standard is supposed to have one (get and set functions).
I don't know Python, so I don't know what you're talking about... I want to set a default table value instead of "undefined", I only have one table and no objects, so I don't have to inherit anything; it's almost exactly like the following:

Code: Select all

var t = ['NULL', 'Mark', 'Jack'];
n = {USERCOUNT};  //I don't know if this syntax is common or if it's relative to the forum I'm editing, anyway that gets a number equal to the number of users
for (i = 1; i <=n; i++) {
document.write(t[i]+"<br />");
}
If the Usercount increases and I don't update the table (I can't get it automatically) I get:

Code: Select all

Mark
Jack
undefined
but I'd prefer to set something else instead of undefined...
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: [Javascript] __Index metamethod

Post by Inny »

I think the closest approximation is the Firefox extentions to javascript, one of which is __noSuchMethod__. However, being both javascript and firefox, and not lua or love, it's probably way off topic for this forum. You might want to search around on StackOverflow for a while to see what answers you can find.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: [Javascript] __Index metamethod

Post by Nixola »

Inny wrote:it's probably way off topic for this forum
That's why I posted in "General" and not in Support ;)
Anyway, if it's only Firefox that can help me I think that "undefined" instead of the username will be ok. Thanks, anyway
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests