2015-01-31 3 views

답변

2

개체를 반환하는 경우 변수에 저장 한 다음 개체 속성에 액세스 할 수 있습니다.

//function definition 
function fun1(){ 
return{"success": true, "message": "Password changed."}; 
} 

//function calling 
var res1 = fun1(); 

//using the result returned by function call 
if(res1.success)//true 
{ 
    alert(res1.message);//"Password changed. 
} 
1

그것은 단지 대상 일뿐입니다. 속성에 액세스하기 만하면됩니다.

var obj = foo(); 
for(var key in obj) 
    console.log(key, " = ", obj[key]); 

또한 단지 obj.successobj.message 이후의 값을 반환 할 수 있습니다.

0

"다중 반품"이 아닙니다. 그것은 속성을 가진 객체를 반환합니다. 따라서 호출 코드는 객체를 수신 한 다음 해당 속성을 사용합니다.

var a = theFunction(); 
console.log(a.success); 
console.log(a.message); 
-1

이렇게하면?

0
function functionReturningObject(){ 
    return{"success": true, "message": "Password changed."}; 
} 

// use a temporary variable!!! this is a valid syntax but will execute twice 
//  success = functionReturningObject().success 
//  message = functionReturningObject().message 
var map = functionReturningObject(); 

// when you know it contents you refer it directly 
console.log(map.success) 
console.log(map.message) 

// when you do not know it contents, you can "explore" it 
Object.keys(map).forEach(function(key){console.log(key);console.log(map[key]);}) 
관련 문제