Computer Science, asked by daksh1704, 3 months ago

Explain javascript concept

Answers

Answered by Anonymous
0

Answer:

JavaScript is a dynamic computer programming language. It is lightweight and most commonly used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. It is an interpreted programming language with object-oriented capabilities.

Answered by karthik4086
3

Answer:

Image for postImage for post

Image source from codeburst.iofunction Bike(model,color){

this.model = model,

this.color = color,

this.getDetails = function(){

return this.model+' bike is '+this.color;

}

}

var bikeObj1 = new Bike('BMW','BLACK');

var bikeObj2 = new Bike('BMW','WHITE');

console.log(bikeObj1.getDetails()); //output: BMW bike is BLACK

console.log(bikeObj2.getDetails()); //output: BMW bike is WHITE

In the above example, we are creating two objects, bikeObj1, bikeObj2 using a constructor. In JavaScript, every object has its own methods and properties. In our example, two objects have two instances of the constructor function getDetails(). It doesn’t make sense having a copy of getDetails doing the same thing.

Instead of using a copy of instances, we are going to use the prototype property of a constructor function.

Prototype

When an object is created in JavaScript, JavaScript engine adds a __proto__ property to the newly created object which is called dunder proto. dunder proto or __proto__ points to the prototype object of the constructor function.

function Bike(model,color){

this.model = model,

this.color = color

}

Bike.prototype.getDetails = function(){

return this.model+" bike is "+this.color;

}

var bikeObj1 = new Bike(‘BMW’,’Black’);

console.log(bikeObj1.getDetails());

The bikeObj1 object, which is created using the Bike constructor function, has a dunder proto or __proto__ property which points to the prototype object of the constructor function Bike.

In below code, both bikeObj1 it's dunder proto or __proto__ property and Bike.prototype property is equal. Let’s check if they point at the same location using === operator.

console.log(bikeObj1.__proto__ === Bike.prototype );

//output : true

TK [Not sure what the author intends to say here] So using prototype property, how many objects are created functions are loaded into memory only once and we can override functions if required.

2. JavaScript(ES6) Class

JavaScript classes, introduced in ES6, are primarily syntactical sugar over JavaScript’s existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript. In early ES5 using function expression.

Similar questions