Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

object constructor

Posted on 2011-10-10
2
Medium Priority
?
234 Views
Last Modified: 2012-05-12
Hi


I was reading an example regarding the object constructor on
"http://www.javascriptkit.com/javatutors/oopjs2.shtml
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");
                cat.speak();
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.

Regards
Karan Gupta
0
Comment
Question by:KaranGupta
2 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36942033
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
0
 
LVL 13

Accepted Solution

by:
darren-w- earned 2000 total points
ID: 36942097
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 );
        return;
    }

    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



http://www.codeproject.com/KB/aspnet/JsOOP1.aspx

Here it shows its usages in a more practical context
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

572 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