Java Script special Flashcards
JavaScript Prototype
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)
Explain - this
“This” is dynamic and references the object /left on the point/ that is currently calling the function.
“This” In the Global context
“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());
“This” In the Function context
1. Function invocation
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
“This” In the Function context
2. Method invocation
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
“This” In the Function context
2. Constructor invocation
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());
“This” In the Function context
2. Indirect invocation
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 “]);
Example - prototype + this
Example - arrow function
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));