GithubHelp home page GithubHelp logo

webrtcgame / variableevents.js Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 144 KB

Micro factory to add properties to javascript objects that contain get/set/modonget/modonset user specified functions.

License: MIT License

JavaScript 100.00%

variableevents.js's Introduction

Variables with events

Micro factory to add properties to javascript objects that contain get/set/modonget/modonset user specified functions.

It'll just let you assign functions for when:

A property is accessed.

A property is set/changed.

Example fiddle: http://jsfiddle.net/x9EmX/15/

Example linking a progress element to a variable: http://jsfiddle.net/BBs7Q/

It simply uses Object.defineProperty

But this let's you do some neat things, such as evented changing of some thing when a value on an object changes...

Let's say you're making a game and you want to have a HP var on your character linked to always update a HP progress bar.
You can constantly check in your update loop to make sure it's updated, or you can just use this and have it automatically update any time the HP var on the character changes.

Neat stuff.

example:

  var o = function () {};
  
  
  VariableEvents(o, "fun", 5, function (x) {
      alert("it's retreived as " + x); //event for when it's set, this case the default of 5
  }, function (x) {
      alert("it's changed to " + x); //event when it's changed
  }, function (x) {
      return x * 10; // modifier of the set value
  }, function (x) {
      return x / 5; // modifier of the return value
  });
  
  alert("Value of fun: " + o.fun); //accessed and modified
  
  o.fun = 6; //change var to 6, but is then modified by the set modifier function above
  
  alert("Value of fun: " + o.fun); //return value after modified by return value modifier
  
  o.fun = 7; //change var to 7 which is then modified

variableevents.js's People

Contributors

webrtcgame avatar

Stargazers

Jarrod Roberson avatar

Watchers

James Cloos avatar  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.