Skip to content

Instantly share code, notes, and snippets.

@neoGeneva
Created September 14, 2011 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neoGeneva/1216184 to your computer and use it in GitHub Desktop.
Save neoGeneva/1216184 to your computer and use it in GitHub Desktop.
C# like properties in JavaScript
var createProperty = function(getSet) {
var _value;
var autoImplement = {
get: function() { return _value; },
set: function(value) { _value = value; return _value; }
};
getSet = getSet || autoImplement;
var get = getSet.get || function() { throw new Error('No get method defined for this property.'); },
set = getSet.set || function() { throw new Error('No set method defined for this property.'); };
return function(value) {
if (value !== undefined)
return set(value);
return get();
};
};
@ozair-kafray
Copy link

Well, what I was looking for something like:
myClass.customProperty = 123; // sets value
var value = myClass.customProperty; // gets value

Note the difference from the last two lines of your example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment