https://github.com/iminside/js.private
Private properties for objects
https://github.com/iminside/js.private
Last synced: 8 months ago
JSON representation
Private properties for objects
- Host: GitHub
- URL: https://github.com/iminside/js.private
- Owner: iminside
- Created: 2015-09-15T06:23:36.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-09-22T08:28:25.000Z (almost 11 years ago)
- Last Synced: 2025-02-16T08:16:35.359Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 160 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Install
```
npm install js.private --save
```
### Usage
```javascript
import Private from "js.private";
class People {
constructor( firstname, lastname, gender, city, street ){
$( this ).firstname = firstname; // private property
$( this ).lastname = lastname; // private property
$( this ).address.country = country; // private property
$( this ).address.city = city; // private property
this.gender = gender; // public property
}
get name(){
return $( this ).generateName(); // call private method
}
get info(){
return this.gender + ", " + $( this ).age;
}
get address(){
return $( this ).address.city + ", " + $( this ).address.street;
}
}
const $ = Private({
firstname: "", // Default values
lastname: "",
age: 28,
address: {
country: "",
city: ""
},
generateName: function(){
return $( this ).firstname + " " + $( this ).lastname;
}
});
let ivan = new People( "Ivan", "Ivanow", "man", "Russia", "Moscow" );
ivan.name // > "Ivan Ivanov"
ivan.info // > "man, 28"
ivan.address // > "Russia, Moscow"
let anna = new People( "Anna", "Ananina", "woman", "Germany", "Berlin" );
ivan.name // > "Anna Ananina"
ivan.info // > "woman, 28"
ivan.address // > "Germany Berlin"
ivan.lastName // > undefined
anna.gender // > "woman"
ivan.generateName // > undefined
```