Simulated JavaScript` this in Lua

I have always liked the way in Javascript that you can set the value of this pointer through f.call(newThisPtrValue). I wrote something in lua to do this, it Works:

_G.call = function(f, self, ...)
local env = getfenv(f)
setfenv (f, setmetatable({self = self}, {__index = env}))
local result = {f(...)}
setfenv(f, env)
return unpack( result)
end

There are a few points I am not sure about:

>I hope unpack({…}) has performance overhead. Is there a solution?
>Is it possible to severely disrupt the functional environment in any way?
>Is this a really bad idea?

Lua’s pseudo OOP has a great advantage that it is already very easy to implement:

local Person = {}
function Person:create( firstName, lastName )
local person = {firstName=firstName, lastName=lastName }
setmetatable(person,{__index=self})
return person
end
function Person:getFullName()
return self.firstName .. "" .. self.lastName
end
local me = Person:create( "Gavin", "Kistner" )
local you = Person:create( "Eric", "Someone" )
print( me :getFullName() )
--> "Gavin Kistner"
print( me.getFullName( you) )
--> "Eric Someone"

I wrote An article discussing this issue (other than that):
Learning Lua: Pseudo-OOP Syntax and Scope.

Edit: This is a continuing example like jQuery:

local Array = {}
function Array:new(...)
local a = {...}
setmetatable(a,{__index =self})
return a
end
function Array:each(callback)
for i=1,#self do
callback(self[i],i ,self[i])
end
end
function Array:map(callback)
local result = Array:new()
for i=1,#self do
result [i] = callback(self[i],i,self[i])
end
return result
end
function Array:join(str)
return table.concat(self,str)
end

local people = Array:new( me, you )

people:each( function(self,i)
print(self:getFullName())
end )
--> "Gavin Kistner"
--> "Eric Someone"

print( people:map(Person.getFullName):join(":") )
--> "Gavin Kistner:Eric Someone"

I always like to join In Javascript, you can use f.call(newThisPtrValue) to set the value of this pointer. I wrote something in lua to do this, and it works:

_G.call = function(f, self, ...)
local env = getfenv(f)
setfenv(f, setmetatable({self = self}, {__index = env} ))
local result = (f(...))
setfenv(f, env)
return unpack(result)
end

How many Point I’m not sure:

>I hope unpack({…}) has performance overhead. Is there a solution?
>Is it possible to severely disrupt the functional environment in any way?
>Is this a really bad idea?

A good advantage of Lua’s pseudo OOP is that it is very easy to implement:

< pre>local Person = {}
function Person:create( firstName, lastName )
local person = {firstName=firstName, lastName=lastName }
setmetatable(person,{__index=self})
return person
end
function Person:getFullName()
return self.firstName .. “” .. self.lastName
end
local me = Person:create( “Gavin”, “Kistner” )
local you = Person:create( “Eric”, “Someone” )
print( me:getFullName() )
–> “Gavin Kistner”
print( me.getFullName( you) )
–> “Eric Someone”

I wrote an article discussing this issue (in addition to ):
Learning Lua: Pseudo-OOP Syntax and Scope.

Edit: This is a continuous example like jQuery:

local Array = {}
function Array:new(...)
local a = {...}
setmetatable(a,{__index=self})
return a
end
function Array:each(callback)
for i=1,#self do
callback(self[i],i,self[i])
end< br />end
function Array:map(callback)
local result = Array:new()
for i=1,#self do
result[i] = callback(self[i],i,self[i])
end
return result
end
function Array:join(str)
return table.concat(self,str)
end

local people = Array:new( me, you )

people:each( function(self,i)
print(self:getFullName())
end )
- -> "Gavin Kistner"
--> "Eric Someone"

print( people:map(Person.getFullName):join(":") )
--> "Gavin Kistner:Eric Someone"

Leave a Comment

Your email address will not be published.