# property.js

> A library for simply working with JavaScript Properties, including observability and privacy

Latest version **0.0.2** (published 2013-06-13) · BSD license · 0 weekly downloads

## Install

```sh
npm install property.js
pnpm add property.js
yarn add property.js
bun add property.js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2013-06-13 |
| First published | 2013-06-10 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Robert Herber |
| Maintainers | herber |
| Keywords | property, observable, privacy |

## Links

- npm: https://www.npmjs.com/package/property.js
- Repository: https://github.com/herber88/property.js
- npm.io page: https://npm.io/package/property.js

## Recent versions

- 0.0.2 (latest) — 2013-06-13
- 0.0.1 — 2013-06-10

## README

property.js
===========

property.js is a JavaScript Library that Provides Simpler Usage of the Native Properties (as specified in ECMAScript 5, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty for browser support).

### The main features are:
 * Create properties in a simple way
 * Observe changes to the properties
 * Use privacy for separation of concern

## Creation
		var HiSayer = function(){
			property.mixin(this);
			
			this.addProperty("phrase", "hello, my name is");
			this.addProperty("name", "Roberto");
			this.addProperty("greeting").
				getter(function(){ return this.phrase + " " + this.name; }).
				setter(false);
		}

		var hiSayer = new HiSayer();
	
		console.log(hiSayer.greeting); //console: hello, my name is Roberto
		hiSayer.name = "Alejandro";
		console.log(hiSayer.greeting); //console: hello, my name is Alejandro

## Observe
		var HiSayer = function(){
			property.mixin(this);
			this.addProperty("phrase", "hello, my name is");
			this.addProperty("name", "Roberto").observe(function(newVal, oldVal){
				console.log("Did you just change name from " + oldVal + " to " + newVal + "?")
			});
		}

		var hiSayer = new HiSayer();

		hiSayer.name = "Alejandro"; //console: Did you just change name from Roberto to Alejandro?

## Privacy
		var HiSayer = function(){
			property.mixin(this);	
			var phrase 		= this.addProperty("phrase", "hello, my name is"),
				name 		= this.addProperty("name", "Roberto").setter(true, true), //second argument specifies that setter is private
				greeting 	= this.addProperty().
					getter(function(){ return this.phrase + " " + this.name; }, true).
					setter(false);

			name("Alejandro"); //Use the private handle to set, this uses same syntax as ex. JQuery Properties
			console.log(greeting()); //Use the private handle to get!
		}

		var hiSayer = new HiSayer();

		console.log(hiSayer.greeting); //Undefined because property is fully private
		hiSayer.name = "Alejandro"; //throws error: Attempt to change private setter publicly

## Alternate creation (without mixin)
		var HiSayer = function(){
			property(this, "phrase", "hello world");
			console.log(this.phrase);	
		}

## Getters and setters
The Getters and Setters are powerful, and allows you to use the properties in a flexible manner: 
- The first argument allows you to specify a custom Getter/Setter Method, or alternatively a truthy value to use the default Method (directly getting and setting the internal value of the property), or a falsy value to remove the Getter/Setter completely. Both getters and setters are expected to return a value, for the setter this is the value that's gonna get stored internally.
- The second argument specifies whether the Property is private or not (public is default). A private Setter/Getter is only accessable from the private property handle (which is the variable returned from addProperty), whereas a public Setter/Getter will be available as a property on the object.

---
_Source: https://npm.io/package/property.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
