Object Orientation JS Flashcards

1
Q
// What does the below code print out?
    console.log(this);
A

The default value for this is the global object

The Global Object (window in a browser)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
// What does the below code print out?
    "use strict";
    (function (){
      console.log(this);
    })();
A

In use strict mode the default value of this is undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
// What does the below code print out?
    "use strict";
    var animal = {
      kind: "Cow"
      which: function () {
        console.log(this.kind);
      }
    };
    animal.which();
A

Cow

Yes, “this” in this situation points to the animal object itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
// What does the below code print out?
    "use strict";
    var animal = {
      kind: "Cow"
      which: function () {
        console.log(this.kind);
      }
    };
    var animalFunc = animal.which;
    animalFunc();
A

It throws an error

Because we are in use strict mode and not calling the function directly from the animal object, this is now undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
// What does the below code print out?
    "use strict";
    function sayHello(last_name) {
      console.log("Hello " + this + " " + last_name);
    }
    sayHello("Hussain");
A

In strict mode this defaults to undefined

Hello undefined Hussian

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
// What does the below code print out?
    "use strict";
    function sayHello(last_name) {
      console.log("Hello " + this + " " + last_name);
    }
    sayHello.call("Asim","Hussain");
A

Hello Asim Hussian

The first param to call is used as the this variable and the second and onwards are the params to the function being called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
// What does the below code print out?
    "use strict";
    function sayHello(last_name) {
      console.log("Hello " + this + " " + last_name);
    }
    sayHello.apply("Asim",["Hussain"]);
A

Hello Asim Hussian

The first param to the apply function is used as the “this” variable, the array is then used for each of the params to the function being called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
// What does the below code print out?
    "use strict";
    function sayHello(last_name) {
      console.log("Hello " + this + " " + last_name);
    }.bind("Asim");
    sayHello("Hussain");
A

Uncaught syntaxErro: unexpected Token

bind can only be used on functions after they have been created and assigned to a variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
// What does the below code print out?
    "use strict";
    var sayHello = function(last_name) {
      console.log("Hello " + this + " " + last_name);
    }.bind("Asim");
    sayHello("Hussain");
A

Hello Asim Hussian

bind can be used on function expressions to fix the value of this regardless of how the function is called later on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
// What does the below code print out?
var device = {
  kind:"music player"
};
var product = Object.create(device);
console.log(product.kind);
A

music player

Javascript searches up the prototype chain to find the kind property on the device object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you mimic a class in JS

A

Functional Constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

// what is the output..

“use strict”;

var dude = person(‘adam’,’c’);

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};
A

output: uncaught typeError: cannot set property ‘first_name’ undefined

this is global window object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

// what is the output..

“use strict”;

var dude = new person(‘adam’,’c’);

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};
A

No errors

The ‘new’ keyword create a new object so doesn’t use the global window object

Similar to call function

var dude = {};
person.call(dude, "adam","cadieux");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Add function to this sudo class

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};
A
function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
   this.full_name = function() {
         return this.first_name + ' ' + this.last_name;
   };
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Add function to this sudo class using prototype

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};
A

notice .prototype

person.prototype.full_name_prototype = function() {
return this.first_name + ‘ ‘ + this.last_name;
};

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantage to using the prototype method

A

saves on memory

multiple instances all share the same prototype

17
Q

Disadvantage to using the sudo internal class function

A

Adding the same full name function will repeat it for every time an instance is created. Bloat up the application

18
Q

Advantage of using the sudo internal class function

A

private member variable

19
Q

How to set the first name so it is always the same

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};
Only in:
sudo  internal class function
A

use closure

// remove this in function, it will get set in closure

function person(first_name, last_name) {

this. first_name = first_name;
this. last_name = last_name;

this.full_name = function() {
return first_name + ‘ ‘ + this.last_name;
};
};

20
Q

the keyword class which version of JS

A

ES6

21
Q

What does call in a function call Person.call(dude,”adam”,”Cadieux”)

A

Every function has a function called ‘call’ which calls the function (what that function will use as this) . Has to be first item in function call

22
Q

create function Professional using person

function person(first_name, last_name) {
   this.first_name = first_name;
   this.last_name = last_name;
};

person.prototype.full_name_prototype = function() {
return this.first_name + ‘ ‘ + this.last_name;
};

A
function Professional(title,first_name,last_name) {
   person.call(this,first_name,last_name); //call the base 
   this.title = title;
}
23
Q

add new function to Professional to get full name and title. (inheritance)

function Professional(title,first_name,last_name) {
   person.call(this,first_name,last_name); //call the base 
   this.title = title;
}
A

Professional.prototype = Object.create(Person.prototype);

creates a link from Professional prototype to Person prototype

Professional.prototype.professional_name = function() {
return this.title + ‘ ‘ + this.first_name + ‘ ‘ + this.last_name;
};

24
Q

Create Person Object Prototype OO

with return full_name function

A
var Person = {
    full_name: function() {
  return  this.first_name + ' ' + this.last_name;
   }
}
25
Q

Prototype OO call Person Object

A

var adam = Object.create(Person);

26
Q

Prototype OO

Add first_name and last_name to Person Object

call

A

var Person = {

init: function (first_name, last_name) {
this.first_name = first_name;
this.last_name = last_name;
return this;
};

full_name: function() {
   return  this.first_name + ' ' + this.last_name;    } }
27
Q

Prototype OO

create adam person and initialize with first and last name

A
var adam = Object.create(Person);
adam.init("adam","cadieux");
28
Q

Prototype OO

create adam person and initialize with first and last name without calling the init function (long way)

A

Can remove init for function

var adam = Object.create(Person, 
  {
   first_name: {
      value: 'adam'  
      },
  last_name: {
       value:'cadieux'
   }
  )};
29
Q

Prototype OO

create adam person and initialize with first and last name Factory

A
function PersonFactory(first_name, last_name) {
   var person = Object.create(Person);
   person.first_name = first_name:
    person.last_name = last_name;
    return person;
}
30
Q

Prototype OO call person factory

A

var adam =PersonFactory(“adam”,”cadieux”);

31
Q
// How would you create an instance of this pseudo-class using the constructor pattern?
    function Device(kind) {
      this.kind =  kind;
    }
    var product = new Device("music player");
    var product = Device("music player");
    var product = Device.call("music player");
A

var product = new Device(“music player”);

32
Q
// What would the below code print out?
    function Device(kind) {
      this.kind =  kind;
      this.printKind = function () {
        console.log(this.kind)
      }
    }
    var product = new Device("music player");
    product.printKind();
A

Music Player

33
Q
// What would the below code print out?
    function Device(kind) {
      this.kind =  kind;
      this.printKind = function () {
        console.log(kind)
      }
    }
    var product = new Device("music player");
    product.kind = "radio";
    product.printKind();
A

Music Player

The printKind function is a closure, it refers to the kind argument passed into the function constructor (and not this.kind).

34
Q
// What would the below code print out?
    function Device(kind) {
      this.kind =  kind;
    }
    Device.prototype.printKind = function () {
      console.log(this.kind);
    };
    var product = new Device("music player");
    product.printKind();
A

Music Player

Javascript looks for the printKind function and finds it on the prototype of the product object.

35
Q
// What does the below code print out?
    var Device = {
      kind:"Music Player"
    };
    var AppleProduct = Object.create(Device
     {
      name: "iPod"
    });
    var purchase = Object.create(AppleProduct);
    console.log(purchase.name);
A

Uncaught TypeErrorL

The second param to the Object.create function is an object literal that described the properties we want to create on the object. So the value should be something like {name: { value: ‘iPod’ }} and not {name:’iPod’}