GithubHelp home page GithubHelp logo

rednick16 / getset.lua Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joshtynjala/getset.lua

1.0 0.0 0.0 7 KB

A simple getter and setter library for Lua.

Home Page: https://github.com/joshtynjala/getset.lua

Lua 100.00%

getset.lua's Introduction

getset.lua

A library for defining getters and setters on Lua tables by Josh Tynjala.

Usage

local getset = require("getset")

function createSimple2DPoint()
	local _x = 0
	local _y = 0
	local point = {}
	getset.defineProperty( point, "x",
	{
		get = function()
			return _x
		end,
		set = function( value )
			_x = value
		end
	})
	getset.defineProperty( point, "y",
	{
		get = function()
			return _y
		end,
		set = function( value )
			_y = value
		end
	})
	
	getset.defineProperty(point, "length",
	{
		get = function()
			return math.sqrt(_x * _x + _y * _y)
		end
		-- no setter required
	})
	
	-- prevents new fields from being added later, and existing fields cannot
	-- be configured (we can't redefine the getters and setters later).
	-- see also: getset.preventExtensions()
	return getset.seal(point)
end

local myPoint = createSimple2DPoint()
myPoint.x = 4
myPoint.y = 4

-- returns 5.6568542494924:
local myPointLength = myPoint.length

-- runtime error because the table is not extensible:
myPoint.z = 7 

-- runtime error because length has no setter:
myPoint.length = 9 

-- runtime error because table is sealed:
getset.defineProperty( myPoint, "x",
{
	get = function()
		return 1
	end
})

API

The getset library offers the following functions. For more detailed descriptions, see the documentation above each function in getset.lua.

getset.defineProperty( table, key, descriptor )

Defines a getter and a setter on a table for a specific key. Both are optional.

getset.preventExtensions( table )

Prevents new properties from being added to a table. Existing properties may be modified and configured.

getset.isExtensible( table )

Determines if a table is may be extended.

getset.seal( table )

Prevents new properties from being added to a table, and existing properties may be modified, but not configured.

getset.isSealed( table )

Determines if a table is sealed.

getset.lua's People

Contributors

joshtynjala avatar rednick16 avatar

Stargazers

Salman Selim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.