-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThis&Call.js
More file actions
62 lines (44 loc) · 1.52 KB
/
This&Call.js
File metadata and controls
62 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Call Method
/*
The call() method calls a function with a given "this"
value arguments provided individually.
SYNTAX
fun.call(object, arg1, arg2,..)
*/
var obj = {num: 2};
var addToThis = function(a, b, c){
return this.num + a + b + c;
};
addToThis.call(obj, 1, 2, 3);
// 8
var person1 = { firstName: 'John', lastName: 'Doe'};
var person2 = {firstName: 'Jane', lastName: 'Doe'};
function hello (greeting) {
console.log(greeting + '' + this.firstName + '' + this.lastName);
}
hello.call(person1, 'Hello');
// call method review
// calls a function with a given "this" value and arguments provided individually
// call - indevidual arguments
// apply - array argument
// C(all) - arrguments separated by commas
// A(apply) - arguments of an array
// Syntax
//function.call(thisContextObject, arg1, arg2...)
// Practice
var myLanguages = function(lang1, lang2, lang3) {
console.log('My name is ' + this.name + ' and I know ' + lang1 + ', ' + lang2 + ', and ' + lang3);
};
// challenge
// create two people objects with a name property and value
// use the call method on our function
// pass person object as the first argument followed by arguments for lang1, lang2 and lang3
var person1 = {
name: 'Tim'
}
myLanguages.call(person1, 'JavaScipt', 'Ruby', 'Python');
// My name is Tim and I know JavaScipt, Ruby, and Python
var person2 = { name: 'Sarah'};
var languages = ['English', 'Spanish', 'German'];
myLanguages.call(person2, languages[0], languages[1], languages[2]);
// My name is Sarah and I know English, Spanish, and German