object constructor

Posted on 2011-10-10
Last Modified: 2012-05-12

I was reading an example regarding the object constructor on
for practicing the first heading i.e. object constructor and prototyping I have created one small example

 function Animal(name) {            
                    this.speak = function() {
                        alert("Hi " + name);

                var cat = new Animal("sweetu");
which is working fine. But a quick question comes to my mind is what is the practical usage of this concept and if I use it what will be the benefit.

Karan Gupta
Question by:KaranGupta
    LVL 17

    Expert Comment

    You can create the number of properties of class "Animal". In above example, class "Animal" has the property speak. In real world scenario, object has multiple properties. You can set the properties of that object using above example
    LVL 13

    Accepted Solution

    Here is a more realistic object based example:

    function MyClass()
        var m_data = 5;
        var m_text = "Hello World";
        this.SetData = SetData;
        this.SetText = SetText;
        this.ShowData = DisplayData;
        this.ShowText = DisplayText; 
        function DisplayData()
            alert( m_data );
        function DisplayText()
            alert( m_text );
        function SetData( myVal ) 
            m_data = myVal;
        function SetText( myText ) 
            m_text = myText;
    var myClassObj1 = new MyClass();
    var myClassObj2 = new MyClass();
    myClassObj1.SetData( 10 );
    myClassObj1.SetText( "Obj1:  Hello World" );
    myClassObj2.SetData( 20 );
    myClassObj2.SetText( "Obj2:  Hello World" );
    myClassObj1.ShowData();    // displays: 10
    myClassObj1.ShowText();    // displays: "Obj1:  Hello World"
    myClassObj2.ShowData();    // displays: 20
    myClassObj2.ShowText();    // displays: "Obj2:  Hello World"

    Open in new window

    Here it shows its usages in a more practical context

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
    The viewer will learn how to count occurrences of each item in an array.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now