mirror of
https://github.com/flynx/object-run.js.git
synced 2025-10-28 10:30:08 +00:00
added conditional mode...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
42e45a4e61
commit
45efca9089
13
README.md
13
README.md
@ -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>
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
@ -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
15
run.js
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user