Object Orientation JS Flashcards
// What does the below code print out? console.log(this);
The default value for this is the global object
The Global Object (window in a browser)
// What does the below code print out? "use strict"; (function (){ console.log(this); })();
In use strict mode the default value of this is undefined
// What does the below code print out? "use strict"; var animal = { kind: "Cow" which: function () { console.log(this.kind); } }; animal.which();
Cow
Yes, “this” in this situation points to the animal object itself
// What does the below code print out? "use strict"; var animal = { kind: "Cow" which: function () { console.log(this.kind); } }; var animalFunc = animal.which; animalFunc();
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
// What does the below code print out? "use strict"; function sayHello(last_name) { console.log("Hello " + this + " " + last_name); } sayHello("Hussain");
In strict mode this defaults to undefined
Hello undefined Hussian
// What does the below code print out? "use strict"; function sayHello(last_name) { console.log("Hello " + this + " " + last_name); } sayHello.call("Asim","Hussain");
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
// What does the below code print out? "use strict"; function sayHello(last_name) { console.log("Hello " + this + " " + last_name); } sayHello.apply("Asim",["Hussain"]);
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
// What does the below code print out? "use strict"; function sayHello(last_name) { console.log("Hello " + this + " " + last_name); }.bind("Asim"); sayHello("Hussain");
Uncaught syntaxErro: unexpected Token
bind can only be used on functions after they have been created and assigned to a variable
// What does the below code print out? "use strict"; var sayHello = function(last_name) { console.log("Hello " + this + " " + last_name); }.bind("Asim"); sayHello("Hussain");
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
// What does the below code print out? var device = { kind:"music player" }; var product = Object.create(device); console.log(product.kind);
music player
Javascript searches up the prototype chain to find the kind property on the device object.
How do you mimic a class in JS
Functional Constructor
// 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; };
output: uncaught typeError: cannot set property ‘first_name’ undefined
this is global window object
// 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; };
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");
Add function to this sudo class
function person(first_name, last_name) { this.first_name = first_name; this.last_name = last_name; };
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; }; };
Add function to this sudo class using prototype
function person(first_name, last_name) { this.first_name = first_name; this.last_name = last_name; };
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; };