internal-data
Description
Private properties implementation using WeakMap as described on MDN - Private Properties.
Synopsis
TypeScript with WeakMap
Actually this library does nothing more than exporting an interface for TypeScript.
This interface just extends WeakMap by overriding its get()
method to return I
instead of I | undefined
, because
data should already be set in constructor.
There is no need to do type assertion in every method to get internal data.
; const internalData: InternalDataInterface<MyClass Internal> = ; type Internal = password: string ; name: string; { const internal: Internal = password ; internalData; thisname = name; } : boolean const internal = internalData; return internalpassword === password; const object = 'George' '1234'; const name = objectname; // -> Georgeconst password = objectpassword; // -> TYPESCRIPT ERRORconst isValid = object; // -> true
JavaScript with Function Interface
; const getInternal = InternalData; { const internal = ; thisname = name; internalpassword = password; } { const internal = ; return internalpassword === password; } const object = 'George' '1234'; const name = objectname; // -> Georgeconst password = objectpassword; // -> Undefinedconst isValid = object; // -> true
JavaScript with Object Interface
; const internalData = ; // eslint-disable-line no-use-before-define { const internal = internalData; thisname = name; internalpassword = password; } { const internal = internalData; return internalpassword === password; } const object = 'George' '1234'; const name = objectname; // -> Georgeconst password = objectpassword; // -> undefinedconst isValid = object; // -> true
Details
To access private properties of the object use getInternal(this)
method. It returns an object to store private attributes
of given object (In this case it is It is this
)
API
InternalData
Class which provides private data storage.
Kind: global class
Hideconstructor:
- InternalData
- new InternalData()
- instance
- .get(object) ⇒
Object
- .get(object) ⇒
- static
- .getFunction() ⇒
GetInternalFn
- .getFunction() ⇒
new InternalData()
Constructor
Object
internalData.get(object) ⇒ Returns private data object for given object.
Kind: instance method of InternalData
Returns: Object
-
Private data of given object.
Param | Type | Description |
---|---|---|
object | Object |
Object to get private data for. |
GetInternalFn
InternalData.getFunction() ⇒ Returns a function to access private data object.
Kind: static method of InternalData
Returns: GetInternalFn
-