Skip to content

Commit 2a37faa

Browse files
author
littleknightgames
committed
got this.js doing its example thingies
1 parent 91d6cc2 commit 2a37faa

1 file changed

Lines changed: 40 additions & 5 deletions

File tree

‎assignments/this.js‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,61 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. is it a function called by new?
5+
* 2. using bind call or apply
6+
* 3. is it a global function?
7+
* 4. are you calling a method?
8+
89
*
910
* write out a code example of each explanation above
1011
*/
1112

1213
// Principle 1
1314

1415
// code example for Window Binding
16+
var myFunction = function() {
17+
console.log(this);
18+
}
1519

20+
myFunction();
1621
// Principle 2
1722

1823
// code example for Implicit Binding
24+
function foo(){
25+
console.log(this.a);
26+
}
27+
28+
var obj = {
29+
a:2,
30+
foo:foo
31+
};
1932

33+
obj.foo();
2034
// Principle 3
35+
function Foo() {
36+
37+
this.name = 'bar';
38+
this.say = function () {
39+
return "I am " + this.name;
40+
};
41+
}
42+
43+
var name = 'foo';
44+
var result = new Foo();
45+
console.log(result.name);
2146

2247
// code example for New Binding
2348

2449
// Principle 4
2550

26-
// code example for Explicit Binding
51+
// code example for Explicit Binding
52+
53+
function greet() {
54+
console.log(this.name);
55+
}
56+
57+
var person = {
58+
name:'bar'
59+
}
60+
61+
greet.call(person, arg1, arg2, arg3, ...);

0 commit comments

Comments
 (0)