0

I know that java script is dynamic lang and I wonder if there is option to return something similar to

 Inte.prototype.getM = function(sMethod) {
     return  this._mMet[sMet].ret && 
     return this._mMeth[sMet].ret.default;
  };

Currently when I try it as-is I got error(which is not surprising :)) .

3
  • 5
    create another object which contains these two values and return it ! Commented Dec 30, 2014 at 12:08
  • 2
    Return an object (or an array) Commented Dec 30, 2014 at 12:08
  • 1
    push them into array and return it Commented Dec 30, 2014 at 12:09

6 Answers 6

2

You can return with an array:

 return  [this._mMet[sMet].ret,this._mMeth[sMet].ret.default];

or by an object:

return  {'one': this._mMet[sMet].ret, 'two': this._mMeth[sMet].ret.default};

EDIT

Based on OP comment, he want some validation on those values:

if (typeof this._mMet[sMet].ret == 'undefined' || typeof this._mMeth[sMet].ret.default == 'undefined') { 
    return false; 
} else { 
    return  {'one': this._mMet[sMet].ret, 'two': this._mMeth[sMet].ret.default};
}
Sign up to request clarification or add additional context in comments.

3 Comments

just last question assume that i Use the second how should I verify that the this._mMet[sMet].ret && this._mMet[sMet].ret.defult have values since when they undefiend I got error
Before the return. just do an if statement. if (typeof this._mMet[sMet].ret == 'undefined' || typeof this._mMeth[sMet].ret.default == 'undefined') } //do something for eg return false, } else { //add them to the object and return}
can you please update it as answer
1

May be you need to use array of objects? For example:

var array = [];
array.push(this._mMet[sMet].ret);
array.push(this._mMet[sMet].ret.default);
return array;

Comments

0

You can return only one thing. But it can be array:

Inte.prototype.getM = function(sMethod) {
     return  [this._mMet[sMet].ret, this._mMeth[sMet].ret.default];
  };

Comments

0

You can return an object

Inte.prototype.getM = function(sMethod) {
     return {
         ret: this._mMet[sMet].ret,
         retDefault: this._mMeth[sMet].ret.default
    };
};

Then you can access it using

var returnObject = inte.getM(arguements);
var ret = returnObject.ret //equvalent to returnObject['ret'];
var retDefault= returnObject.retDefault //equivalent to returnObject['retDefault'];

Comments

0

Best way for this is

function a(){
     var first=firstObject;
     var second=secondObject;
     return {first:first,second:second};
}

Then use

a().first // first value
a().second // second value

Comments

0

Try to retun as array:

Inte.prototype.getM = function(sMethod) {
     return new Array(this._mMet[sMet].ret,this._mMeth[sMet].ret.default);
};

Comments