Solved

Seeking expert opinions on Javascript data object class

Posted on 2014-02-26
3
172 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
updateParent() HTML Javascript 21 61
listing all functions in JavaScript 19 103
addressing a specific html page 9 25
Asp.net mvc 5 5 17
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now