Quick question about javascript

GarettM

Posting Freak
Aug 5, 2010
833
136
I have been playing around with javascript for about a week now and I am starting to feel a little comfortable with it, still kinda weird but its not that bad, The hardest part I have been dealing with is classes in JavaScript, is there a proffered way on declaring classes? This is how I have been doing it so far...

Code:
/**
 * @constructor
 * @name Person
 */
var Person = function Person(name, age) {
    this.name = name;
    this.age = age || 0;
};
/**
 * @function
 * @name Person#getName
 */
Person.prototype.getName = function () {
    return this.name;
};
/**
 * @function
 * @name Person#setName
 */
Person.prototype.setName = function (name) {
    this.name = name;
};
/**
 * @function
 * @name Person#getAge
 */
Person.prototype.getAge = function () {
    return this.age;
};
/**
 * @function
 * @name Person#setAge
 */
Person.prototype.setAge = function (age) {
    this.age = age;
};
/**
 * Declaring a new Person as Ted with the name of "Ted" and age of "15"
 */
var Ted = new Person("Ted", 15);
//
var logmsg = "Created a new Object [ted] = Person(" + Ted.getName() + ", " + Ted.getAge() + ")";
// log to console
console.log(logmsg);
// log to html tag
var consolediv = document.getElementById('console');
consolediv.innerHTML = '<p>' + logmsg + '</p>';

And my other question what javascript resources or frameworks should I try to learn?
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
ES6 brings a lot of great updates in JavaScript, and also in how you can work with classes. Do keep in mind that not every browser supports it yet. You could use something like Babel to transpile it to ES5.

This is quite a good read:
 
Last edited:

Adil

DevBest CEO
May 28, 2011
1,276
714
ES6 brings a more familiar OO approach to JS but it's still a Prototypical language at heart

What I would suggest is trying out a variety of different styles, learning about prototypes and es6 and seeing what you feel comfortable with
You might wanna have a look at Typescript too
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,921
As @Wess mentioned, ES6 has , although to get cross browser support you'll want to use for the foreseeable future (assuming you're building something for the browser, and not something you'd run server side).

If you wanted to make your pseudo-class slightly more like an actual class you could do the following:
Code:
/**
 * @constructor
 * @name Person
 */
var Person = function(name, age) {
   this.name = name;
   this.age = age || 0;
};

Person.prototype = {
  /**
   * @function
   * @name Person#getName
   */
  getName: function () {
   return this.name;
  },

  /**
   * @function
   * @name Person#setName
   */
  setName: function (name) {
    this.name = name;
  },
};
 

GarettM

Posting Freak
Aug 5, 2010
833
136
As @Wess mentioned, ES6 has , although to get cross browser support you'll want to use for the foreseeable future (assuming you're building something for the browser, and not something you'd run server side).

If you wanted to make your pseudo-class slightly more like an actual class you could do the following:
Code:
/**
 * @constructor
 * @name Person
 */
var Person = function(name, age) {
   this.name = name;
   this.age = age || 0;
};

Person.prototype = {
  /**
   * @function
   * @name Person#getName
   */
  getName: function () {
   return this.name;
  },

  /**
   * @function
   * @name Person#setName
   */
  setName: function (name) {
    this.name = name;
  },
};
I thought about doing that, but doesn't that make it harder calling a method, from another method?
 

Users who are viewing this thread

Top