Search This Blog

JavaScript Important

01. Class Inheritance












02. Object Inheritance











03. Prototype in Javascript

If we are created a variable, array, function or object, a prototype obviously there. After creating that, when we are going to access a parameter or trying to add or remove parameter from the that obviously we are calling through the prototype.

It looks like

'__proto___'

Factory Function

A factory function can be defined as a function that creates an object and returns it. It is similar to constructor functions/class functions. The factory function is a very useful tool in JavaScript since it returns the object of any class directly.

function Employee(name) {
return {
name: name,
work: function () {
console.log('New Employee name: ' + name);
}
};
}

Factory Function and Prototype

Here we are creating a Factory function for vehicle and it has speak function as well. Then we are trying to access factory function through the 3 different methods to get 3 different answers. according to the img. it returned the method as function type. It mean we are used lot of memory and this will not a good practise.

Through the prototype we can create a common access function and through that we can address desired 3 different responses.

Vehicle.prototype.speak = function(word){
console.log(word);
}

So according to the second img, we can see the calling object type is not a function.

Class and Prototype

As same as the function base prototype, class with prototype also reduce the memory usage without creating additional function over it.

same as the third img, when we creating a class with a constructor, we can access a method which is created inside the class and accessing outside of the object without creating a additional function on it.



04. Javascript call() method

This method comes under the Inheritance concept. If you need to access or get the function from another object without defining the same thing inside a function or object, Easily you can do through the 'call' method in javascript.

(required_function).call(desired_object)

define Object1
defie Object2

let address(){
console.log(this.villageName+" "+this.town)
}
address.call(Object1)
address.call(Object2)

so if you need to pass the additional parameter through the 'call' method, you can do it as follows,


let address(homeNo){
console.log(homeNo+" "+this.village+" "+this.town)
}
address.call(Object1, "1/231")
address.call(Object2, "2/450")

05. Javascript apply() method

This method comes under the Inheritance concept. The difference is comparing to the call method, in apply method you have to pass the additional parameters through an array.

(required_function).apply(desired_object)

define Object1
defie Object2 let address(homeNo, landOwner){
console.log(homeNo+" "+this.village+" "+this.town)
}
address.apply(Object1, ["1/231", "Winlson"])
address.apply(Object2, ["2/450", "Saku"])

06. Javascript mind() method

This method comes under the Inheritance concept. This method bind the relavent function and create a copy of function use to later on

(required_function).bind(desired_object)

let copyAddress = address.bind(Object1, "1/231", "Winlson")
console.log(copyAddress)
copyAddress (); //calling the function

No comments:

Post a Comment

Unlock Web Testing Excellence: Comprehensive Cypress Cheat Sheet !

  Unlock the Full Potential of Cypress with Our Comprehensive Cheat Sheet! Welcome to my blog!   If you're a QA engineer or a test autom...

Most popular