-3

I have a function that returns an object in javascript. The objects returned are instances of a "messagePack" that I want to send to the server. Each messagePack will be different. E.g.:

    function someFunc() {
        return {
            name: "chatMessage",
            time: Date.now(),
            data: "yooo hoooo",
            hash: hashFunc()               
        }
    }

I then create a create a messagePack and add it to an array:

    var messagePack = someFunc();

    msgArray.push(messagePack)

Is this ok? Should I be creating a constructor function for message pack and using the new keyword? I've seen this done in as similar situation but I can't see why what I'm doing wont work.

7
  • note: hash is not being used for security. Commented Jan 15, 2017 at 11:21
  • Better to create a constructor function , so that you can set new name and data for every new instance Commented Jan 15, 2017 at 11:21
  • This seems like a question that is either entirely opinion based or could be reworked to make it on-topic for the Code Review Stackexchange. Commented Jan 15, 2017 at 11:21
  • @suzo you don't need a constructor or new, the object returned by the function will always be a new instance of a literal object. You can still pass arguments to the someFunc() function if you want to change name or data. Commented Jan 15, 2017 at 11:25
  • And indeed I am passing arguments to someFunc(). Can a moderator move this please if it is incorrectly categorised. Commented Jan 15, 2017 at 11:31

2 Answers 2

0

A constructor is useful, when youve got multiple objects containing the same data, e. g. functions or constants. As you dont have this repeatin data ( i believe) , you dont need them. if youve got repeating data, you could do this:

var message_prot={
     name: "chatMessage",
        time: Date.now(),
        data: "yooo hoooo",
        hash: false
};

function somefunc(){
  a=Object.create(message_prot);
  a.name="Hi";
  a.hash=hashfunc();
  return a;
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Afaik, this is still a factory, not a constructor function. A constructor will take advantage of this, doesn't return an object but this and must be called with new
@Booster2ooo: but techincally no difference to a constructor ( output, internal design)
indeed, that's why the anwser to Your Friend---A Rat is "no", you can do it without using an "actual" constructor.
may read my answer again. i say that his solution is working perfeclty.
-1

This is a factory pattern, it's perfectly correct. You don't need to use a constructor.

2 Comments

To clarify, here I am creating a "new Object" every time I return the literal right? So there's no need for a constructor.
That's right, each call to someFunc() will return a new literal object. Factories are a good way to work, it avoids the use of this which can be confusing some times. Douglas Crockford made a really good speech about javascript functions: youtube.com/watch?v=ya4UHuXNygM (at 1:00:00 he covers constructors and inheritance but the whole speech is interesting)

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.