Last active
August 29, 2015 13:56
-
-
Save scottwarren/8973575 to your computer and use it in GitHub Desktop.
example of .call method
This file contains 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
var model = { | |
departureLocation: { | |
port: 'Some Port', | |
city: 'Tokyo', | |
country: 'Japan' | |
}, | |
arrivalLocation: { | |
post: 'Main Port', | |
city: 'Seattle', | |
country: 'United States' | |
}, | |
success: function() { | |
console.log('if this logs, you failed'); | |
}, | |
visitedCities: '[visitedCities]', | |
inclusions: '[inclusions]', | |
longDescription: '[longDescription]', | |
dummyFetch: function(options) { | |
if (options.success) { | |
var response = {response: 'This is the response...'}; | |
// change context of method call with .call - read more, here: | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call | |
// or here: | |
// http://ejohn.org/apps/learn/#25 | |
options.success.call(this, response, options); | |
} | |
} | |
}; | |
var yolo = { | |
example: 'test', | |
visitedCities: 'exampleData', | |
success: function(response, options) { | |
console.log(this); // model, rather than yolo object | |
console.log(response); // dummy response that was built in the dummyfetch method of the model | |
}, | |
swag: function() { | |
model.dummyFetch({ | |
success: this.success | |
}); | |
} | |
}; | |
yolo.swag(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment