forked from and-digital/and-workshop-corejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_pass_by.js
45 lines (34 loc) · 1.04 KB
/
09_pass_by.js
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
/*
It's always pass by value, but for objects the value of the variable is a reference.
Because of this, when you pass an object and change its members, those changes persist outside of the function.
This makes it look like pass by reference. But if you actually change the value of the object variable you will see that the change does not persist, proving it's really pass by value.
*/
function changeStuff(num, obj1, obj2) {
num = num * 10;
obj1.item = 'changed';
obj2 = { item: 'changed' };
}
let num = 10;
let obj1 = { item: 'unchanged' };
let obj2 = { item: 'unchanged' };
changeStuff(num, obj1, obj2);
console.log(num); //10
console.log(obj1.item); //changed
console.log(obj2.item); //unchanged
function identify() {
return this.name.toUpperCase();
}
function speak() {
var greeting = "Hello, I'm " + identify.call(this);
console.log(greeting);
}
var me = {
name: 'Kyle'
};
var you = {
name: 'Reader'
};
identify.call(me); // KYLE
identify.call(you); // READER
speak.call(me); // Hello, I'm KYLE
speak.call(you); // Hello, I'm READER