Solved

Seeking expert opinions on Javascript data object class

Posted on 2014-02-26
3
175 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
  • 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 42

Accepted Solution

by:
Rob Jurd, EE MVE 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 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn how to dynamically set the form action using jQuery.
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…

776 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