Skip to content

Commit c2498a5

Browse files
committed
finished this.js
1 parent ca4950d commit c2498a5

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

recursion.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ while (n <= 10) {
1010
// write a recursive - function called countToTen that mimics the while loop above.
1111

1212
// code here
13-
13+
function coundToTen(n){
14+
if(n === 10){
15+
return;
16+
}
17+
console.log(n);
18+
coundToTen(++n);
19+
}
1420
// when you code is ready, un-comment the next line and run the file
15-
// console.log(countToTen());
21+
console.log(countToTen(1));
1622
/* ================ Next Problem ================= */
1723

1824
// Problem 2:
@@ -28,6 +34,11 @@ const factorial = n => {
2834
console.log(factorial(5));
2935

3036
// write the above function in a recursive way.
31-
37+
function recursiveFactorial(n){
38+
if(n === 1){
39+
return 1;
40+
}
41+
return n * recursiveFactorial(n-1);
42+
}
3243
// when your code is ready, un-comment the next line and run the file
33-
// console.log(recursiveFactorial());
44+
console.log(recursiveFactorial(10));

this.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
3-
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
8-
*
3+
* this keyword have 4 scopes.
4+
* 1. Global Scope
5+
in global scope the this keyword points to the global object in system it is windows object and in a browser its browser object.
6+
if we have not binded the object to any other object implicitly its this scope will be the global.
7+
8+
* 2. Implicit binding scope
9+
implicit binding is when we invoke this keyword within a method of any object. In this case every first child method, its this will point to the object itself.
10+
if we have any inner object inside a object the this points to the global object.
11+
12+
* 3. Explicit binding scope
13+
Explicit binding is when we use call , apply or bind to any function. we define what the this keyword will point to in the first parameter of call, apply and bind.
14+
15+
* 4. New keyword binding scope
16+
this is when we use a constructor function to create new objects, and then, this keyword points to the parameter passed into the constructor.
917
* write out a code example of each explanation above
1018
*/
1119

@@ -14,15 +22,63 @@ console.log('hello world!');
1422
// Principle 1
1523

1624
// code example for Window Binding
25+
console.log(this) // here this is the window object.
1726

1827
// Principle 2
1928

2029
// code example for Implicit Binding
30+
let newObj = {
31+
name: 'imran',
32+
age: 30,
33+
getAgeAndName: function(){
34+
return this.name + ' ' + this.age; // here this points to the newObj(parent object);
35+
child: function any(){
36+
console.log(this); // but here this will be the global object(window);
37+
}
38+
}
39+
}
2140

2241
// Principle 3
2342

2443
// code example for New Binding
44+
function ConstExample(param1, param2) {
45+
this.name = param1; // here this points to the parameter passed
46+
this.age = param2;
47+
}
48+
49+
let childObj = new ConstExample('imran' , 30);
50+
console.log(childObj) // ---> {name: imran, age: 30}
2551

2652
// Principle 4
2753

2854
// code example for Explicit Binding
55+
56+
function any(param1, param2){
57+
return this.name + ' ' + this.age + 'and i like to ' + param1 + 'and' + param2;
58+
}
59+
60+
let testObj = {
61+
name: 'imran',
62+
age: 30
63+
};
64+
65+
66+
let testparams = ['code', 'music' ];
67+
//call example
68+
// here we are setting the this of any() to testObj1, note the this.age and param2 will return undefined;
69+
console.log(any.call(testObj, 'code'));
70+
71+
72+
//apply example
73+
// same as call , but expects an array as parameter.
74+
//call and apply both invokes the any();
75+
console.log(any.apply(testObj, testparams))
76+
77+
78+
// bind example
79+
// same as call and apply , expects a parameter or comma seperated parameters(not array) as second argument.
80+
//but unlike call and apply it creates another version of the function(any()), and dose not invoke the newly created function.
81+
//we can use a variable to capture the newly created function with the this keyword specified to a object.
82+
//this new funciton whenever we invoke will always point to the testObj as its this .
83+
let bindTest = (any.bind(testObj, 'code'));
84+
console.log(bindTest())

0 commit comments

Comments
 (0)