1

I am a bit of a JS noob, but have good .net experience in my pocket.

I am working on a mobile website/app, and I have collected some data similar to business listings from a custom web service.

On .net, I would have, at this time, started building a custom class consisting of a list of other "business-listing-classes". I would then step through each custom object in my list, and use it on the screen.

Two questions:

  • Should my javascript approach be different than above?
  • Can this be done in Javascript?

I know javascript does not have classes persè, and I know you can have a custom function act as a "class". Am I then just to instantiate a bunch of different versions of said custom function, and add all of it to an array?

I almost have the feeling that I should just stick with them in the json format they are? Or build simple arrays? (From what I've learned from Javascript so far..)

Am I over engineering this? I am not sure what the correct javascript approach is.

15
  • I'm not sure where you read that javascript does not have classes. You should read some more. This is a good reference: developer.mozilla.org/en-US/docs/Web/JavaScript/… Commented Jun 8, 2015 at 8:48
  • 1
    JavaScript has no class, @Louis is right. JS has prototype inheritance. And object oriented does not mean classes. Commented Jun 8, 2015 at 8:50
  • 1
    EcmaScript 5 (current) does not have classes. EcmaScript 6 (soon) will. Commented Jun 8, 2015 at 8:57
  • 2
    Well I would, and I am :-) Even in ES6, which introduce the class statement, there will be no class, and under the hood, it will still be prototype inheritance. It will only be syntactic sugar. You can invoke functions as constructors, yes, but that does not make it a class, with a 'contract'. In JS, objects are instantiated from objects (from functions, which are objects), not classes. Commented Jun 8, 2015 at 9:02
  • 1
    @Yellen Hehe, I know at least a dozen of Java-class-like implementations in JavaScript, from the John Resig's April fools joke one to the maybe more serious one from javascriptMVC, and of course the prototype one, to point to a few. Needless to say, I am not fond of any. Still, Louis is right saying that "JavaScript does not have classes per se". Commented Jun 8, 2015 at 9:42

1 Answer 1

2

It honestly depends what you want to do with this array

If you are going to send it via an XMLHTTPRequest then I'd say you should use JSON format.

var arr_of_objs = [{},{}]; //fill it up however you need it

If this array is going to consist of objects with the same attributes/properties and you were going to use this client side, creating "classes" is optimal.

var person = function(o){
  this.name = o.name || 'unknown';
  this.height = o.age || -1;
}
var arr_of_classes = [];
for(var i = 0; i < 10; i++){
  arr_of_classes.push(new person({
    name: 'name' + i,
    age: 10 + i
  }));
}

Why stop there? You could even create a custom class to handle instances.

    (function(){
      var person = function(o){
        this.name = o.name || 'unknown';
        this.height = o.age || -1;
      }
      var personHandler = function(){
        if(!(this instanceof personHandler)){
          return new personHandler();
        }
        this.persons = [];
      }
      personHandler.fn = personHandler.prototype = {
        load: function(){
          for(var i = 0; i < 10; i++){
            this.persons.push(new person({
              name: 'name' + i,
              age: 10 + i
            }));
          }
        },
        remove: function(index){
          //do stuff
        }

      }

   //export it
   window.personHandler = personHandler;

    })();

Then you would simply.

personHandler().load();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.