Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

// problem #1
// convert the Animal constructor function from 'constructors.js' into an ES6 class

class Animal {
constructor(params) {
this.name = params.name;
}
grow () {
`${this.name} grew larger!`;
}
}

// problem #2
// convert the Cat constructor function from 'constructors.js' into an ES6 class


class Cat extends Animal {
constructor(params) {
super(params);
}
}
// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution

// const foofie = new Cat({
// name: 'foofie',
// });
//
// foofie.grow();
const foofie = new Cat({
name: 'foofie',
});

foofie.grow();

17 changes: 11 additions & 6 deletions constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function Animal(options) {
this.name = options.name;
}


// add 'grow' to Animal's prototype here
Animal.prototype.grow = function(){
console.log(`${this.name} grew larger!`);
}

// problem #2
// setup Cat to inherit from Animal
Expand All @@ -18,16 +22,17 @@ function Animal(options) {

function Cat(options) {
// invoke Animal here with .call
Animal.call(this,options);
}

// connect the prototypes here

Cat.prototype = createObject(Animal.prototype);
// if everything is setup properly the code below will print 'Foofie grew larger!'
// uncomment the code below to test your solution

// const foofie = new Cat({
// name: 'foofie',
// });
//
// foofie.grow();
const foofie = new Cat({
name: 'foofie',
});

foofie.grow();

19 changes: 15 additions & 4 deletions recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ while (n <= 10) {
// write a recursive - function called countToTen that mimics the while loop above.

// code here

function coundToTen(n){
if(n === 10){
return;
}
console.log(n);
coundToTen(++n);
}
// when you code is ready, un-comment the next line and run the file
// console.log(countToTen());
console.log(countToTen(1));
/* ================ Next Problem ================= */

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

// write the above function in a recursive way.

function recursiveFactorial(n){
if(n === 1){
return 1;
}
return n * recursiveFactorial(n-1);
}
// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial(10));
67 changes: 61 additions & 6 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
*
* this keyword have 4 scopes.
* 1. Global Scope
in global scope the this keyword points to the global object in system it is windows object and in a browser its browser object.
if we have not binded the object to any other object implicitly its this scope will be the global.

* 2. Implicit binding scope
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.
if we have any inner object inside a object the this points to the global object.

* 3. Explicit binding scope
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.

* 4. New keyword binding scope
this is when we use a constructor function to create new objects, and then, this keyword points to the new object being created.
* write out a code example of each explanation above
*/

Expand All @@ -14,15 +22,62 @@ console.log('hello world!');
// Principle 1

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

// Principle 2

// code example for Implicit Binding
let newObj = {
name: 'imran',
age: 30,
getAgeAndName: function(){
return this.name + ' ' + this.age; // here this points to the newObj(parent object);
child: function any(){
console.log(this); // but here this will be the global object(window);
}
}
}

// Principle 3

// code example for New Binding
function ConstExample(param1, param2) {
this.name = param1; // here this points to the new object (childObj);
this.age = param2;
}

let childObj = new ConstExample('imran' , 30);
console.log(childObj) // ---> {name: imran, age: 30}

// Principle 4

// code example for Explicit Binding

function any(param1, param2){
return this.name + ' ' + this.age + 'and i like to ' + param1 + 'and' + param2;
}

let testObj = {
name: 'imran',
age: 30
};


let testparams = ['code', 'music' ];
//call example
// here we are setting the this of any() to testObj1, note the this.age and param2 will return undefined;
console.log(any.call(testObj, 'code'));


//apply example
// same as call , but expects an array as parameter.
//call and apply both invokes the any();
console.log(any.apply(testObj, testparams))


// bind example
//unlike call and apply it creates another version of the function(any()), and dose not invoke the newly created function.
//we can use a variable to capture the newly created function with the this keyword specified to a object.
//this new funciton whenever we invoke will always point to the testObj as its this . And we can pass parameter to the newly created function.
let bindTest = (any.bind(testObj));
console.log(bindTest('code'))