xhallix
xhallix

Reputation: 3011

Javascript - add same properties with different values

Important : the code works, so this is more a "is this the correct way?" question.

I was trying to add different values to my object properties within a function and push it to an array then. The object properties should remain the same but the name should be changed.

Output:

Object { 
  clientId="cXbHV32Sz0gh-10Mo6DX", 
  userName="Foo"
}, 
Object { 
  clientId="J3ZkTsK6ixTM6pUBo6DY", 
  userName="bar"

I was only able to achieve it with this solution:

var clientObj =  function (clientId, userName){
    var that = this;
    this.clientId = clientId;
    this.user
}

function myFunc(socket.id, userName){
    var clientInfo = new clientObj(socket.id, userName)
    clientList.push(clientInfo)
}

I wonder if I really need to have a constructor here, which does nothing than save my properties.

How can I achieve this without using a Constructor then?

I thought of something like this (but this does only replace the values) :

var ClientObj = {}

function myFunc(socketId, userName){
  ClientObj['socketID'] = socketID
  ClientObj['userName'] = userName
  clientList.push(clientObj)
}

Sorry if this might be a bit confusing

Upvotes: 1

Views: 92

Answers (2)

Jani Hyytiäinen
Jani Hyytiäinen

Reputation: 5407

If I'm understanding you right. You want to use some external object with the same values but different property names and push it to an array. In this case I'd write something like:

function Client(obj) {
    this.clientId = obj.clientId;
    this.user = obj.userName;
    return this;
}

Client.prototype.pushTo = function(arr) {
    arr.push(this);
    return this;
};

var clientList = [];

Then, when I receive an object:

var externalObj = { clientId: 'blah', userName: 'name' };

I could call:

var convertedClient = new Client(externalObj).pushTo(clientList);

You'd get it converted, pushed to the array and assigned it to local variable all with the same one liner. Also, if you later on figure new actions to perform with the same type, you can add them.

Client.prototype.clearName = function(){
    this.name = '';
    return this;
};

If you keep on returning this from your prototypes, you can chain a lot of commands one after another.

var processedClient = new Client(obj).pushTo(arr).clearName();

I'm not sure if this is what you're looking for but there's something to think about. Going through a custom type and having to call a constructor doesn't sound so bad anymore.


UPDATE:


Just out of the fun of it, I decided to do this fiddle to play with: http://jsfiddle.net/Y3eA5/

Upvotes: 1

Adam Rackis
Adam Rackis

Reputation: 83366

Just a few small things. Your that variable is useless at the moment. Usually something like that is used when you need to keep a reference to you object in an Ajax callback.

  • clientObj should be capitalized since it's a constructor function.

  • Consider making clientObj a function declaration so it'll be hoisted, which may or may not be important to you.

So maybe something like this

function ClientObj(clientId, userName){
    this.clientId = clientId;
    this.user = userName;
}

Also, this is invalid:

function myFunc(socket.id, userName){

I think you meant something like this:

function myFunc(socketId, userName){

Upvotes: 1

Related Questions