?
Solved

Seeking expert opinions on Javascript data object class

Posted on 2014-02-26
3
Medium Priority
?
180 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 1500 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 1500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

801 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