Abstract Classes Object reflection Strict Mode JavaScript

How to implement Abstract Classes & Object reflection & Strict Mode In JavaScript?





In this article we will understand How do I create an Abstract Classes & Object reflection & Strict Mode in JavaScript By Sagar Jaybhay.









Abstract Classes









Object-oriented
programming languages like C# and java support abstract classes. These classes
are incomplete and when you trying to create an object of that abstract classes
it will throw a compile-time error.





The abstract classes are mainly for base classes.









var Shape=function(nameofshape)

this.shapeName=nameofshape;
throw new Error("You can't create object of this abstract class.") ;



Shape.prototype.draw=function()

return "Drawing the Shape "+this.shapeName;


var Circle=function(name)

this.shapeName=name;


Circle.prototype=Object.create(Shape.prototype);

var circle=new Circle("Circle");
document.writeln("<br/>");
document.writeln("is circle instance of Circle "+(circle instanceof Circle));
document.writeln("<br/>");
document.writeln("is circle instance of Shape "+(circle instanceof Shape))
document.writeln("<br/>");
document.writeln(circle.draw());








Object.create(Shape);




This method is used to create an object of class without using a constructor.









Abstract Class in JavaScript
Abstract Class in JavaScript








Object reflection In JavaScript









C#, java
like object-oriented programming languages support a reflection.





Reflection allows us to inspect meta-data of assemblies, modules, and types. Since javascript is an object-oriented programming language it also supports reflection.









var Student=function(firstname,lastname,id)

this.FirstName=firstname;
this.LastName=lastname;
this.Id=id;


Student.prototype.getFullname=function()
return this.FirstName+" "+this.LastName;


Student.prototype.getID=function()
return this.Id;


var stud=new Student("Sagar","Jaybhay",101);

document.writeln("Below are the properties");
for(property in stud)

document.writeln("<br/>");
document.writeln(property);

document.writeln("<HR>");
document.writeln("Below are the properties with values");
for(property in stud)

document.writeln("<br/>");
document.writeln(property+ " : "+stud[property])









Object reflection In JavaScript
Object reflection In JavaScript








Strict Mode In Javascript









ECMAScript
version 5 introduced strict mode to javascript. With this use of strict mode it
easy to detect javascript silent error as they would throw an error. By using
strick mode your debugging is easier and mistakes are reduced. Most modern
browsers support strict mode.





In C# or
Java, these are object-oriented languages like that Javascript also object-oriented
language. But javascript is not very god at reporting errors or throwing an
error.





Suppose in
c# without declaring the type of variable you cant assign value to it. C#
compiler will throw the compile-time error.





Fullname=”sagar”




But in
javascript without declaring variable you can assign value to that variable.
But this behavior you want to suppress.





"use strict";

mystring="sagar";

document.writeln(mystring);








Then you need to use strict mode.









Strict Mode In JavaScript
Strict Mode In JavaScript








To overcome this I declare variable an error goes off.









"use strict";

var mystring="sagar";

document.writeln(mystring);




Strict Mode In JavaScript 1
Strict Mode In JavaScript 1








You can write a strict mode in function also means you want to monitor a function and rather than typing the use strict in the whole script file you can type inside the function like below.









var display=function()
"use strict";

var mystring="sagar";

document.writeln(mystring);



display();








The rules are applied inside the function and if you write any variable outside the function that will work regular one.









Summary





By using above code example you will able to understand How do I create an abstract base class & Object reflection & Strict Mode in JavaScript By Sagar Jaybhay.










GitHub Project Link :- https://github.com/Sagar-Jaybhay/JavaScript-All-Labs










Comments

Popular posts from this blog

How to Start a YouTube Channel Free - Complete Guide

How Angular know which module is the startup module?

How to Start a YouTube Channel Free - Complete Guide