1.0.1 • Published 5 years ago
@kingjs/property-descriptor.initialize.extension v1.0.1
@kingjs/property-descriptor.initialize.extension
Lazily declare an accessor or function but only for specific polymorphic types.
Usage
var assert = require('assert')
var initializeExtension = require('@kingjs/property-descriptor.initialize.extension');
function capitalize() {
var firstChar = this.charAt(0);
// capitalize
firstChar = firstChar.toUpperCase();
var result = firstChar + this.substring(1, this.length);
return result;
}
var Capitalize = Symbol(capitalize.name);
Object.defineProperty(
Object.prototype,
Capitalize,
initializeExtension.call(
{ value: capitalize },
Capitalize,
() => String
)
);
var test = 'foobar'[Capitalize]();
assert(test == 'Foobar');
var descriptor = Object.getOwnPropertyDescriptor(String.prototype, Capitalize);
assert(descriptor.value == capitalize);
assert.throws(() => [][Capitalize]());
API
extension(this, name, callback())
Parameters
this
: A descriptor describing an accessor or function.name
: The symbol of the property being described.callback
: Returns a typethis
must be polymorphic with before patching the definition.
Returns
A descriptor whose accessors or function has been replaced with a stub which checks that this
is polymorphic with the extended type before patching and invoking the accessor or function.
Install
With npm installed, run
$ npm install @kingjs/property-descriptor.initialize.extension
Source
https://repository.kingjs.net/property-descriptor/initialize/extension
License
MIT
1.0.1
5 years ago