0% found this document useful (0 votes)
1 views

JS-7

The document outlines various methods for creating objects in JavaScript, including object literals, constructor functions, and Object.create(). It also explains object prototype methods, object chaining, the differences between fat arrow functions and normal functions, the advantages of using Axios over other HTTP clients, and the concept of closures. Each section includes examples to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

JS-7

The document outlines various methods for creating objects in JavaScript, including object literals, constructor functions, and Object.create(). It also explains object prototype methods, object chaining, the differences between fat arrow functions and normal functions, the advantages of using Axios over other HTTP clients, and the concept of closures. Each section includes examples to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1: Different Ways of Creating Objects in JavaScript:

1. Object Literal:

javascript
Copy code
const person = {
name: 'John',
age: 30,
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};

2. Constructor Function:

javascript
Copy code
function Person(name, age) {
this.name = name;
this.age = age;
this.greet = function() {
console.log(`Hello, my name is ${this.name}`);
};
}
const john = new Person('John', 30);

3. Object.create():

javascript
Copy code
const personPrototype = {
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
const john = Object.create(personPrototype);
john.name = 'John';
john.age = 30;

Question 2: Object Prototype Methods:

Object prototype methods are methods that are defined on the prototype object of an object
constructor. These methods are shared among all instances of the constructor function and
can be accessed using dot notation.

Example:

javascript
Copy code
function Person(name) {
this.name = name;
}

Person.prototype.greet = function() {
console.log(`Hello, my name is ${this.name}`);
};
const john = new Person('John');
john.greet(); // Output: Hello, my name is John

Question 3: Object Chaining in JavaScript:

Object chaining is a technique where multiple methods or properties are accessed in a chain-
like manner on an object. It improves code readability and reduces the need for temporary
variables.

Example:

javascript
Copy code
const calculator = {
value: 0,
add(num) {
this.value += num;
return this;
},
subtract(num) {
this.value -= num;
return this;
},
multiply(num) {
this.value *= num;
return this;
},
getValue() {
return this.value;
}
};

const result = calculator.add(5).multiply(2).subtract(3).getValue();


console.log(result); // Output: 7

Question 4: Main Difference between Fat Arrow Function and Normal Function:

• Fat Arrow Function:


o Lexical this: this inside a fat arrow function refers to the this value of the
surrounding scope.
o No binding of this: Cannot be bound, so this always refers to the lexical
context.
o Cannot be used as constructors or with arguments object.
• Normal Function:
o Dynamic this: this inside a normal function depends on how the function is
called.
o Can be bound using bind(), call(), or apply().
o Can be used as constructors or with arguments object.

Example to Explain "this" with Fat Arrow Function and Normal Function:

javascript
Copy code
const obj = {
name: 'John',
normalFunc: function() {
console.log(this.name); // Output: John
},
arrowFunc: () => {
console.log(this.name); // Output: undefined (lexical this)
}
};

obj.normalFunc(); // Output: John


obj.arrowFunc(); // Output: undefined

Question 5: Advantages of Axios vs Other Competitors:

• Simpler API: Axios provides a cleaner and easier-to-use API compared to fetch and
other competitors.
• Automatic JSON Parsing: Axios automatically parses JSON responses, reducing
manual parsing.
• Interceptors: Axios allows intercepting and transforming requests and responses.
• Browser Compatibility: Axios supports older browsers and has better cross-browser
compatibility.
• Error Handling: Axios has built-in error handling and response validation features.
• Cancellation: Axios supports cancellation of requests, which can be useful in certain
scenarios.

Question 6: Closures in JavaScript:

A closure is a function defined inside another function (outer function) and has access to
variables declared in the outer function, even after the outer function has finished executing.

Example:

javascript
Copy code
function outerFunction() {
const message = 'Hello';

function innerFunction() {
console.log(message);
}

return innerFunction;
}

const myFunc = outerFunction();


myFunc(); // Output: Hello

In this example, innerFunction is a closure that retains access to the message variable of
outerFunction, even though outerFunction has finished executing.

You might also like