1

New to Javascript. Trying to create a datatype (assuming OBJECT is what I am wanting here) definition that can be used between various functions. The definition, as of now, is only variables (no functions etc). The issue I am having is that I want to initially create it, and then later set the properties...

What I would LIKE to do... is have a definition.

// Object Definition
function resultObj = {
    isValid: true,
    nn_name: '',
    account_name: '',
    translated_name: '',
    address1: '',
    city: '',
    state: '',
    zip: '',
    country: '',
    formattedAddress : '',
    auth_string: '',
    error_text: '',
    error_body: '',
    error_type: ''    
};

At some point, I will create an occurrence of that definition.

myData = new resultObj;
... do some processing here...
... set a FEW of the variables
myData.zip = '12345';

How can I create this so that I don't have to pass in all the values (or empty parameters) at creation time?

user1009073
  • 3,160
  • 7
  • 40
  • 82
  • 1
    That is not valid JavaScript...and JavaScript has both constructor functions and optional parameters...and has no concept of user-defined types... – Jared Smith Aug 11 '16 at 15:49
  • 1
    As @JaredSmith said it is not valid, but if you want to continue with your structure you should check [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign). [Browser Support](http://kangax.github.io/compat-table/es6/#test-Object_static_methods_Object.assign) – Bhavik Aug 11 '16 at 15:54

3 Answers3

3

How about you create your original default/initial object and then whenever you have to create an another new/independent object with the very same initial properties, you just copy that old object in new one?

var myData = Object.assign({}, resultObj); 

You can use Object.assign to create an exact copy.

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

var resultObj = {
    isValid: true,
    nn_name: '',
    account_name: '',
    translated_name: '',
    address1: '',
    city: '',
    state: '',
    zip: '',
    country: '',
    formattedAddress : '',
    auth_string: '',
    error_text: '',
    error_body: '',
    error_type: ''    
};

var myData = Object.assign({}, resultObj);
document.write(JSON.stringify(myData));  

Also see: How do I correctly clone a JavaScript object?

Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

It's not necessary to define all the potential properties on an object:

function resultObj(){}
var result = new resultObj()
result.zip = 12345
console.log(result.zip) //=> 12345
jshawl
  • 3,315
  • 2
  • 22
  • 34
  • Although perf is rarely an issue, not defining all the properties at construction time (even if they default to null) means property access is an order of magnitude slower... – Jared Smith Aug 11 '16 at 15:54
0

You could use something like this:

var getResultObj = function(param1, param2) {
    var one = param2 ? param2 : '';
    var two = param2 ? param2 : '';

    var objectToBeReturned = {
        isValid: true,
        nn_name: one,
        account_name: two,
        translated_name: '',
        address1: '',
        city: '',
        state: '',
        zip: '',
        country: '',
        formattedAddress : '',
        auth_string: '',
        error_text: '',
        error_body: '',
        error_type: ''
      };

      return getResultObj;
};

Every time you call getResultObj(); it returns a new instance of your object. But you could also use any other Constructor Pattern pattern.