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
|
## Examples
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -57,6 +66,10 @@ var l = [1, 2, 3, 4, 5]
|
|||||||
<obj>.run(<func>)
|
<obj>.run(<func>)
|
||||||
-> <obj>
|
-> <obj>
|
||||||
-> <return-value>
|
-> <return-value>
|
||||||
|
|
||||||
|
<obj>.run(<cond>, <func>)
|
||||||
|
-> <obj>
|
||||||
|
-> <return-value>
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "object-run",
|
"name": "object-run",
|
||||||
"version": "1.0.1",
|
"version": "1.1.0",
|
||||||
"description": "Implements .run(..) method on object.",
|
"description": "Implements .run(..) method on object.",
|
||||||
"main": "run.js",
|
"main": "run.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
15
run.js
15
run.js
@ -13,10 +13,25 @@
|
|||||||
|
|
||||||
// Run a function in the context of an object...
|
// 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.prototype.run
|
||||||
|| Object.defineProperty(Object.prototype, 'run', {
|
|| Object.defineProperty(Object.prototype, 'run', {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
value: function(func){
|
value: function(func){
|
||||||
|
// conditional run...
|
||||||
|
if(arguments.length > 1
|
||||||
|
&& !arguments[0]){
|
||||||
|
return this }
|
||||||
|
func = [...arguments].pop()
|
||||||
var res = func ?
|
var res = func ?
|
||||||
func.call(this)
|
func.call(this)
|
||||||
: undefined
|
: undefined
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user