Java Script special Flashcards

1
Q

JavaScript Prototype

A

All JavaScript objects inherit properties and methods from a prototype /parent object/, they can be recalled from child to parent.

A variable person is set as an instance of the Object class.
const person = new Object({
name:’Lia’,
age: 25,
greet: function(){
console.log( Hello ${this.name})
}
})

Object.prototype.sayHello = function () {
console.log(‘Hay’);
}
const me = Object.create(person)

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

Explain - this

A

“This” is dynamic and references the object /left on the point/ that is currently calling the function.

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

“This” In the Global context

A

“This” In the Global context, this references the global object, which is the window object on the web browser or global object on Node.js.
console.log(this === window); // true
this.color= ‘Red’;
console.log(window.color); // ‘Red’

function hello() {
console.log(‘Hello’, this);
}

const person ={
name: ‘Lia’,
age: 31,
sayHello: hello
}
console.log(person.sayHello());
console.log( window.hello());

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

“This” In the Function context
1. Function invocation

A

In the non-strict mode, the this references the global object
function show() {
console.log(this === window); // true
}
show();
the same like: window.show();
In the strict mode, JavaScript sets the this inside a function to undefined

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

“This” In the Function context
2. Method invocation

A

When you call a method of an object, JavaScript sets this to the object that owns the method. bind we must call like function()

let car = {
brand: ‘Honda’,
getBrand: function () {
return this.brand;
}
}

let bike = {
brand: ‘Harley Davidson’
}

car.getBrand.bind(bike)() //Harley Davidson

use the bind() method of the Function.prototype object. The bind() method creates a new function whose the this keyword is set to a specified value

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

“This” In the Function context
2. Constructor invocation

A

use the new keyword to create an instance of a function object, you use the function as a constructor.

function Car(brand) {
if (!new.target) {
throw Error(‘Must use the new operator to call the function’);
}
this.brand = brand;
}
Car.prototype.getBrand = function () {
return this.brand;
}

let car1 = new Car(‘Mercedes’);
console.log(car1.getBrand());

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

“This” In the Function context
2. Indirect invocation

A

In JavaScript, functions are first-class citizens, they are objects, which are instances of the Function type.
The Function type has two methods: call() and apply() . You can set this value when calling a function.

// call() - call we not call like function
function getBrand(model) {
console.log(model + this.brand);
}

let mercedes = {
brand: ‘Mercedes’
};
let tesla = {
brand: ‘Tesla’
};

getBrand.call(mercedes, “It’s a “);
getBrand.call(tesla, “It’s an “);

// apply() call we not call like function arguments put in array
The apply() the second argument is an array of arguments
getBrand.call(tesla, [“It’s an “]);

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

Example - prototype + this

Example - arrow function

A

arr = [20, 30, 50, 80];

Array.prototype.multBy = function (x) {
return this.map(function (i) {
return i * x;
});
};

console.log(arr.multBy(80));

///////
arr = [20, 30, 50, 80];

const multBy = (x, array) => {
return array.map(function (i) {
return i * x;
});
};

console.log(multBy(80, arr));

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