added conditional mode...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-03-19 10:54:05 +03:00
parent 42e45a4e61
commit 45efca9089
3 changed files with 29 additions and 1 deletions

View File

@ -33,6 +33,15 @@ var o = {}
})
```
Conditionaly run a function...
```javascript
var o = {}
.run(o.x % 2 == 0,
function(){
this.x_is_odd = true
})
```
## Examples
```javascript
@ -57,6 +66,10 @@ var l = [1, 2, 3, 4, 5]
<obj>.run(<func>)
-> <obj>
-> <return-value>
<obj>.run(<cond>, <func>)
-> <obj>
-> <return-value>
```
```

View File

@ -1,6 +1,6 @@
{
"name": "object-run",
"version": "1.0.1",
"version": "1.1.0",
"description": "Implements .run(..) method on object.",
"main": "run.js",
"scripts": {

15
run.js
View File

@ -13,10 +13,25 @@
// Run a function in the context of an object...
//
// obj.run(func)
// -> res
// -> obj
//
// Run func iff cond is true
// obj.run(cond, func)
// -> res
// -> obj
//
//
Object.prototype.run
|| Object.defineProperty(Object.prototype, 'run', {
enumerable: false,
value: function(func){
// conditional run...
if(arguments.length > 1
&& !arguments[0]){
return this }
func = [...arguments].pop()
var res = func ?
func.call(this)
: undefined