Last active
January 5, 2017 17:46
-
-
Save pokisin/d87fdac78d688d959b616afd1db3dfc4 to your computer and use it in GitHub Desktop.
Callbacks Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function basic( callback ){ | |
console.log( 'do something here' ); | |
var result = 'i am the result of `do something` to be past to the callback'; | |
// if callback exist execute it | |
callback && callback( result ); | |
} | |
function callbacks_with_call( arg1, arg2, callback ){ | |
console.log( 'do something here' ); | |
var result1 = arg1.replace( 'argument', 'result' ), | |
result2 = arg2.replace( 'argument', 'result' ); | |
this.data = 'i am some data that can be use for the callback function with `this` key word'; | |
// if callback exist execute it | |
callback && callback.call( this, result1, result2 ); | |
} | |
// this is similar to `callbacks_with_call` | |
// the only difference is we use `apply` instead of `call` | |
// so we need to pass arguments as an array | |
function callbacks_with_apply( arg1, arg2, callback ){ | |
console.log( 'do something here' ); | |
var result1 = arg1.replace( 'argument', 'result' ), | |
result2 = arg2.replace( 'argument', 'result' ); | |
this.data = 'i am some data that can be use for the callback function with `this` key word'; | |
// if callback exist execute it | |
callback && callback.apply( this, [ result1, result2 ]); | |
} | |
basic( function( result ){ | |
console.log( 'this callback is going to print out the result from the function `basic`' ); | |
console.log( result ); | |
}); | |
console.log( '--------------------------------------------------------------------------------------' ); | |
( function(){ | |
var arg1 = 'i am argument1', | |
arg2 = 'i am argument2'; | |
callbacks_with_call( arg1, arg2, function( result1, result2 ){ | |
console.log( 'this callback is going to print out the results from the function `callbacks_with_call`' ); | |
console.log( 'result1: ' + result1 ); | |
console.log( 'result2: ' + result2 ); | |
console.log( 'data from `callbacks_with_call`: ' + this.data ); | |
}); | |
})(); | |
console.log( '--------------------------------------------------------------------------------------' ); | |
( function(){ | |
var arg1 = 'i am argument1', | |
arg2 = 'i am argument2'; | |
callbacks_with_apply( arg1, arg2, function( result1, result2 ){ | |
console.log( 'this callback is going to print out the result from the function `callbacks_with_apply`' ); | |
console.log( 'result1: ' + result1 ); | |
console.log( 'result2: ' + result2 ); | |
console.log( 'data from `callbacks_with_apply`: ' + this.data ); | |
}); | |
})(); | |
---------------------------------------- RESULT ------------------------------------------------------ | |
do something here | |
this callback is going to print out the result from the function `basic` | |
i am the result of `do something` to be past to the callback | |
-------------------------------------------------------------------------------------- | |
do something here | |
this callback is going to print out the results from the function `callbacks_with_call` | |
result1: i am result1 | |
result2: i am result2 | |
data from `callbacks_with_call`: i am some data that can be use for the callback function with `this` key word | |
-------------------------------------------------------------------------------------- | |
do something here | |
this callback is going to print out the result from the function `callbacks_with_apply` | |
result1: i am result1 | |
result2: i am result2 | |
data from `callbacks_with_apply`: i am some data that can be use for the callback function with `this` key word | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment