Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Seeking expert opinions on Javascript data object class

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

609 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