Solved

Seeking expert opinions on Javascript data object class

Posted on 2014-02-26
3
179 Views
Last Modified: 2014-03-02
My sample is a Person class with two properties -- first name and last name. What I have done does not resemble exactly as explained in any books I've read, but it somehow works for me because the two property variables, firstName and lastName, are private, and they can only be obtained and modified through their respective getters and setters methods.

I'm seeking opinions from you JavaScript experts out there whether you would do it this way as well. If not, why not? Please be forthcoming with your comments and critiques. Thanks.

Here's the code:

<script>
    var Person = function(firstName, lastName) {
        var firstName = firstName;
        var lastName = lastName;
        
        var obj = {
            getFirstName: function() {
                return firstName;
            },
            setFirstName: function(name) {
                firstName = name;
            },
            getLastName: function() {
                return lastName;
            },
            setLastName: function(name) {
                lastName = name;
            },
            toString: function() {
                return firstName + " " + lastName;
            }
        };
        
        return obj;
    };
    
    // Now to use the class
    person1 = Person("John", "Doe");
    person2 = Person("Jane", "Down");

    alert(person1.toString());
    alert(person2.toString());
</script>

Open in new window

0
Comment
Question by:elepil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Sar1973
ID: 39891331
I'm not sure that assigning a variable name the name itself (such as var firstName = firstName;) would make the script work...
0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 39891335
there's no need. see this example below

var Person = function(first_name, last_name) {
  var firstName = first_name;
  var lastName = last_name;

  var obj = {
    getFirstName: function() {
      return firstName;
    },
    setFirstName: function(name) {
      firstName = name;
    },
    getLastName: function() {
      return lastName;
    },
    setLastName: function(name) {
      lastName = name;
    },
    toString: function() {
      return firstName + " " + lastName;
    }
  };

  return obj;
};
function Person2(first_name, last_name) {
  var firstName = first_name;
  var lastName = last_name;


  this.getFirstName = function() {
    return firstName;
  };
  this.setFirstName= function(name) {
    firstName = name;
  };
  this.getLastName= function() {
    return lastName;
  };
  this.setLastName= function(name) {
    lastName = name;
  };
  this.toString = function() {
    return firstName + " " + lastName;
  };


  //return obj;
}
// Now to use the class
person1 = new Person2("John", "Doe");
person11 = new Person2("Jane", "Down");

console.log(person1.firstName);
console.log(person11.toString());
console.log(person1.getFirstName());

Open in new window

0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 500 total points
ID: 39891340
thats also a good point sar1973 makes and you can see in my example I've done the same, is I would never name arguments the same as local variables.

that aside, the use of "var" within the object already gives it private scope. have you seen otherwise?

you've signed your object to a variable and I have used the global scope to define my object.  There is nothing wrong with creating a "namespace" for your object especially if there are going to be other classes etc

eg

var mynamespace = {}; // object

mynamespace.Person = function(first_name, last_name) {
...
};

then you would create your object like this:

var myPersonInstance = mynamespace.Person("rob","tagit");
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

691 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