Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CALL, APPLY, AND BIND
- var person = {
- firstname: 'Jason',
- lastname: 'DeGraaf',
- getFullName: function() {
- var fullname = this.firstname + ' ' + this.lastname;
- return fullname;
- }
- }
- // bind creates a copy of whatever function you're calling it on.
- var logName = function(lang1, lang2) {
- console.log('Logged: ' + this.getFullName());
- console.log('Arguments: ' + lang1 + ' ' + lang2);
- console.log('---------------')
- }.bind(person);
- // var logPersonName = logName.bind(person);
- // call actually calls the function. bind only creates copy
- logName('en ', 'es'); //logPersonName();
- logName.call(person, 'en', 'es');
- //appy wants an array! not a list. but that's the only difference between call and apply
- logName.apply(person, ['en', 'es'])
- var person2 = {
- firstname: 'Jane',
- lastname: 'Doe'
- }
- // FUNCTION BORROWING
- // This crazy set up borrows the 'this' from person object above.
- console.log(person.getFullName.apply(person2));
- // FUNCTION CURRYING
- function mutliply(a,b) {
- return a*b;
- }
- // this bind method permamently sets "a" to 2.
- var multiplyByTwo = mutliply.bind(this, 2);
- console.log(multiplyByTwo(4));
- //Answer will be 8. If .bind was (this, 2, 5) set both a permanent. Answer 10.
- var multiplyByThree = mutliply.bind(this, 3);
- console.log(multiplyByThree(4));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement