Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Objects in javascript

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 370
    Comment on it

    In this blog i am going to discuss about objects in Javascript and how we can use it.
    While writing client side scripting language i.e Javascript we use concept of OOPs(object oriented programming languages) which helps us to use properties of OOPs like
    ----> We can create objects.
    ----> Encapsulate objects inside classes.
    ----> Code written once can be reused.
    ----> Inherit classes through prototyping as well as using object's properties and methods.
    ----> Aggregate  multiple objects inside a single object.
    ----> Supports Polymorphism such as a single function can be used in different ways and gives different outputs.   

    OBJECTS: An object can be anything boolean,number,string,date,math,regular expression,array,function etc.
    The best way to understand what is object is to take a real life example.
    Like computer is a real life object which has properties like resolution,sound,graphics,ram memory etc and methods like shutdown,restart,lock etc.
    Object's properties and methods are always same but property values and how the method is use and when may differs.
    In Javascript these objects are stored in a variable and hence a object is converted into variable type.
    Object defined as variable can have single or multiple values.
    Syntax: Var objectName ="value"; object having single value.
    Syntax: Var objectName ={name1:"value1", name2:"value2", name3:"value3"};  object having multiple values.
    Here name:value is a format of defining properties and methods.


    How objects are created using function:
     ---->we can create objects in javascript using any function.
    ----> while creating object, function should not return any value.
    ---->An object is created using a function by calling it inside the function using  as shown in the example below:

    function computer() {
      this.Ram = 0;
      this.Graphics = 0;
      this.anotherfeatures = 0;
    var PC = new computer();

    while calling new class (i.e the instance of previous class) a keyword new is placed before initial  class  as shown here

    here a variable this is also defined inside the function which references the new object that is going to be constructed.
    Once new class is created we can use it as instance of previous class i.e mycomputer, and now we can define new objects created in following ways:


    computer.prototype.newRam = someValue;
    prototype is a property of class constructor using which we can  give all instances of class computer a single property at the same time with some value.


    How to create methods for objects:
    Methods are those actions that are performed only on the object for which it was called.
    Method holds function values.
    Every object have their own methods.

    See the example below to understand how Methods are created for objects:


    Here computer is object name and shutdown,restart,lock are the methods.

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: