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 :)) .

6 Answers6

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};
}
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • 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 –  Dec 30 '14 at 12:38
  • 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}` – vaso123 Dec 30 '14 at 12:46
  • can you please update it as answer –  Dec 30 '14 at 12:47
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;
netwer
  • 737
  • 11
  • 33
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];
  };
Kirill Pisarev
  • 844
  • 4
  • 11
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'];
Kamran224
  • 1,584
  • 7
  • 20
  • 33
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
Yuyutsu
  • 2,509
  • 22
  • 38
0

Try to retun as array:

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