The ways classes are created, how to manage class structure,inheritance hierarchy in java script will be covered in this blog. OOP concepts can be applied in java Script but it is a bit different than Java.
Classes and Objects.
How to create a class in JavaScript.
Functions in JavaScript act as objects. We use functions to create classes. These functions are known as constructor functions. Difference between a normal function and a constructor
function(conventions created by programmers) is that normal functions are written using camel notation and the first letter of the constructor functions(classes) starts with an uppercase letter
and defined as a noun.
Creating objects and classes.
When creating an object; Objects are clone in JavaScript because class is an object unlike in Java.
- Creating JASON like objects using curly brackets.
 |
Creation of JSON object. |
 |
Console output. |
JSON objects not only have properties and methods it also may include embedded objects.
 |
Embedded object inside a JSON object. |
- Creating objects using 'Object constructor'.
 |
Using Object function and creating an object. |
This method is only used to create a single object. Therefore it is not a good practice to create many objects using this method. Because if there are many objects ; like Banana , Grape etc. this procedure must be used each time when an object is created. Therefore it is not a good practise.
JavaScript objects act as Java object.But classes are created as functions.Therefore Functions(class) act as objects in JavaScript. When function is used with ‘new’ keyword that function acts as a Class.
 |
Creating a class using 'constructor' function. |
- Creating functions using constructor pattern with arguments.
 |
Creating objects using constructor pattern passing arguments. |
"this" keyword represent object context.It is only accessible throughout the class (If it is used inside the class or if it is used as a global it can be accessed globally). "this" keyword is bounded to the object. If "this" keyword is used with a function inside the object it will be bounded to the object where all the object will be having that method. It will be making the object heavier; so we have to make the methods in the object be referred by other objects. this can be achieved by prototype object.
- A constructor function(class) is used with the ‘new’ key keyword when creating an Object.Constructor function is a just another function. vehicle is an object created from Vehicle class.
 |
Creating an object from the class. |
Extra facts.....
Functional Scope => 'var 'defines functional scope. Throughout the function variables defined using 'var' are visible. Even if the variable is defined in a block of a function using var key word it is visible throughout the function not only to the block.
Block scope => variables are visible only with in the block.
ex-:if{},switch(){},while{},for{}
prototype
The ancestor of all the java classes is object class. Therefore there are methods that has been inherited to all java classes by the parent Object class. Such as toString(), .equals(),hashCode(),notifyAll()=>thread related methods etc.
JavaScript also have an ancestor known as object class.prototype is a property that every object has in JavaScript inherited by object class.prototype object points to the object class.But prototype object is in the class level so it can not be accessed by the object instances.
 |
accessing prototype using lass name. |
Object instance also has a prototype it is basically the object instance from which object is being created. Object ‘__proto__’ is where object get its properties inherited from.
Creating methods using prototype object.When the functions are created by using prototype object,it only gives the reference to other objects instances.
 |
Creating a method using prototype. |
Creating an object using Inheritance. Inheritance is done using prototype.Functions prototype is used to inherit properties to object instances.
 |
Inheritance using prototype. |
Comments
Post a Comment