1)call,apply,bind example?
Ans:
a. call Method:
The call method is used to call a function with a given this value and arguments provided individually.
Javascript code:
function greet(name) {
console.log(`Hello, ${name}! I am ${this.role}.`);
}
const person = {
role: 'developer'
};
greet.call(person, 'Alice');
// Output: Hello, Alice! I am developer.
In this example, call invokes the greet function with person as the this value and passes 'Alice' as an argument.
b. apply Method:
The apply method is similar to call, but it accepts arguments as an array.
Javascript code:
function introduce(language1, language2) {
console.log(`I can code in ${language1} and ${language2}. I am ${this.name}.`);
}
const coder = {
name: 'Bob'
};
introduce.apply(coder, ['JavaScript', 'Python']);
// Output: I can code in JavaScript and Python. I am Bob.
Here, apply is used to invoke introduce with coder as this and an array ['JavaScript', 'Python'] as arguments.
c. bind Method:
The bind method creates a new function that, when called, has its this keyword set to a specified value, with a given sequence of arguments preceding any provided when the new function is called.
Javascript code:
function saySomething(message) {
console.log(`${this.name} says: ${message}`);
}
const dog = {
name: 'Buddy'
};
const dogTalk = saySomething.bind(dog);
dogTalk('Woof woof!');
// Output: Buddy says: Woof woof!
In this example, bind creates a new function dogTalk where this is bound to dog. When dogTalk is called with 'Woof woof!', it logs the message with Buddy as the name.
These methods (call, apply, and bind) are often used in JavaScript for manipulating this context and passing arguments to functions in different ways.
2)Rest vs spread?
The rest operator and the spread operator are both introduced in ES6 (ECMAScript 2015) and are represented by three dots (...
). While they may look similar, they serve different purposes.
var s="abcdef"
Object.create()
creates a new object with the specified prototype, whileObject.assign()
copies properties from source objects to a target object.Object.create()
allows you to create an object with a specific prototype, which can be useful for prototypal inheritance.Object.assign()
is commonly used for shallow copying properties from one or more source objects to a target object.Object.assign()
modifies the target object directly, whereasObject.create()
creates a new object.
In summary, Object.create()
is used to create a new object with a specified prototype, whereas Object.assign()
is used to copy properties from source objects to a target object. They serve different purposes and are used in different scenarios.
4)Oops 4 pillars?:
Certainly! Let's illustrate each of the Four Pillars of Object-Oriented Programming (OOP) using JavaScript. a. Encapsulation: Encapsulation involves bundling data and methods into a single unit (class) and controlling access to them. In JavaScript, we can achieve encapsulation using classes and access modifiers. javascript Copy code class Car { #speed; // Private field constructor(speed) { this.#speed = speed; } getSpeed() { return this.#speed; } accelerate(increase) { this.#speed += increase; } } const myCar = new Car(50); console.log(myCar.getSpeed()); // Output: 50 myCar.accelerate(20); console.log(myCar.getSpeed()); // Output: 70 In this example, Car class encapsulates the speed property and methods for getting and accelerating the car's speed. The speed property is encapsulated using a private field (#speed). b. Inheritance: Inheritance allows a class (subclass) to inherit properties and methods from another class (superclass) and extend or override them as needed. javascript Copy code class Animal { speak() { console.log('The animal makes a sound'); } } class Dog extends Animal { speak() { console.log('The dog barks'); } } const dog = new Dog(); dog.speak(); // Output: The dog barks In this example, Dog class inherits the speak() method from the Animal class and overrides it with its own implementation. c. Polymorphism: Polymorphism allows objects to take different forms or exhibit different behaviors based on their context. In JavaScript, we can achieve polymorphism through method overriding. javascript Copy code class Shape { draw() { console.log('Drawing a shape'); } } class Circle extends Shape { draw() { console.log('Drawing a circle'); } } class Square extends Shape { draw() { console.log('Drawing a square'); } } const shapes = [new Circle(), new Square()]; shapes.forEach(shape => shape.draw()); // Output: Drawing a circle // Drawing a square In this example, Circle and Square classes override the draw() method inherited from the Shape class, exhibiting polymorphic behavior when called. d. Abstraction: Abstraction involves hiding the implementation details of an object and exposing only the essential features. In JavaScript, abstraction can be achieved through interfaces or by providing simplified APIs. javascript Copy code class RemoteControl { #device; constructor(device) { this.#device = device; } turnOn() { this.#device.power = true; } turnOff() { this.#device.power = false; } } const tv = { power: false, channel: 1, volume: 50 }; const remote = new RemoteControl(tv); remote.turnOn(); console.log(tv.power); // Output: true In this example, RemoteControl class abstracts the functionality of turning a device on and off, hiding the details of how the device is controlled. The implementation details of the device (tv) are abstracted away from the user.
In JavaScript, polymorphism allows objects of different classes to be treated as objects of a common superclass. This means that different classes can have methods with the same name, and you can call these methods on objects of these different classes, but the behavior will vary based on the specific implementation in each class. This is achieved through method overriding.
Comments
Post a Comment