Skip to content

Instantly share code, notes, and snippets.

@jdaly13
Last active December 17, 2015 09:29
Show Gist options
  • Save jdaly13/5587467 to your computer and use it in GitHub Desktop.
Save jdaly13/5587467 to your computer and use it in GitHub Desktop.
.call example - here we demonstrate that functions themselves are objects and can have properties another function will call the properties of another function
//drinking will call chooseWisely and will look for it's liquor property
//within the liquor property is a wine property that will get output
var drinking = function (who) {
var which_spirit = this.liquor.wine;
console.log(which_spirit) // this will output malbec
return true
};
var chooseWisely = function (ohdeliciouswine, brewski, whiskey) {
var bourbon = "Makers Mark"
var wine = ohdeliciouswine || "pinot noir"
var whiskey = whiskey || bourbon
var beer = brewski || "miller low life"
var func_length = chooseWisely.length
var argument_length = arguments.length
return {
wine:wine,
brew:beer,
spirits:whiskey,
length:func_length,
how_man_args:argument_length
}
} // this function will return an object
chooseWisely.liquor = chooseWisely("malbec", "guinness")
//invoke chooseWisely and assign it a liquor property whoose value is the return value from chooseWisely
// could of easily just been another variable but we avoid another global
drinking.call(chooseWisely); // here drinking is invoked and uses chooseWisely as the value of this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment