2016-06-06 2 views
0

나는 자바 스크립트를 처음 사용하며 매우 혼란 스럽다. 자바 스크립트 : 참조에 객체를 저장하는 방법

나는

var recipe = {name:"", price:10, details:"", img_path:""}; 

객체를 생성하고 난 생성자처럼 뭔가 기능을 만들었습니다. 또한, 객체의 속성에

function recipeInstantiation(name, price, details, img_path){ 
     recipe.name = name; 
     recipe.price = price; 
     recipe.details=details; 
     recipe.img_path = img_path; 
     // I dont know what to return here 
     //maybe return recipe; 
} 

값을주고, 그때

VAR의 recipe2 = 레시피 기준으로 레시피 객체를 전달해야;

맞습니까?

+0

recipe.name과 같은 대신 this.name을 사용하고 이것을 반환하십시오. – juvian

+1

모든 개체는 참조입니다. 두 변수를 동일한 객체에 할당하면 두 변수 중 하나를 수정하면 해당 객체에 영향을줍니다. 그래서 그걸로 당신이 묻는 것을 이해하지 못합니다. –

+2

직접 해봤습니까? 여기에 게시하기 전에 자신의 기본 연구를 수행하고 사물을 시도해야합니다. 그런 다음 작동하지 않는 것 또는 혼동스러워하는 특정 것에 대해서만 여기에 게시 할 수 있습니다. – jfriend00

답변

0

객체 참조 형 및 자바 스크립트 언어 구조와 규칙을 따라야 클래스 정의 (즉, 클래스에 대한 파스칼의 경우 사용)

여기 가서 배울 수 있습니다 http://www.w3schools.com/js/js_objects.asp

---------------------------------- 

    //class definition that describes a recipe 
    function Recipe(name, price, details, img_path){ 
    //javascript use this keyword to define data fields. 
      this.name = name; 
      this.price = price; 
      this.details=details; 
      this.img_path = img_path; 

    } 


    function displayRecipe(recipe){ 
     //display recipe as html 
    } 


---------------------------------- 
    var newReceipe = new Recipe("Pasta", 1000, "Something", "img/pasta.jpg"); 
    displayRecipe(newReceipe) 
+2

JavaScript를 배우기 위해 다른 웹 사이트로 이동하는 것 이외에이 답변으로 어떤 OP가 있어야하는지 잘 모르겠습니다. OP는 객체 리터럴 표기법을 사용하고 있습니다. 이유에 대한 추가 정보없이 객체 생성자를 추가했습니다. – Marcus

1

var recipe2 = recipe;

당신은 정확합니다. 위의 경우 recipe2의 값을 수정하면 recipe의 값이 수정됩니다. JavaScript의 여러 유형 (예 : 배열 및 객체)이 참조로 전달되기 때문입니다.

(그냥 참고로, 다른 사용 사례에 참조로 recipe을 통과하지 당신이 , 당신은 사본 recipe의 모든 값,하지만 그 범위를 벗어 났음 새로운 객체를 생성한다 이 질문의.)

+0

참고 당신이 마지막에 대해 옳다는 것이 맞습니다. JS에서는 클래스 생성과 객체 생성과 같은 것이 없다고 가정합니다. – Skemelio

+1

@Skemelio 자바 스크립트에 대해 더 자세히 읽어 보시기 바랍니다.[생성자 함수] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Using_a_constructor_function)는 매우 일반적이며 클래스와 매우 유사합니다. –

+0

많은 노력에 감사드립니다! – Skemelio

1

클래스 (대문자의 첫 글자). 사용이

function Recipe(name, details, ...) { 
    this.name = name, 
    this.details = details, 
    ... 
} 

프로토 타입와 클래스에 기능을 추가 초기화 :

Recipe.prototype.getInfo = function() { 
    return obj; 
} 

전체 예제 :

function Recipe(name, price, details, img_path) { 
     this.name = name; 
     this.price = price; 
     this.details = details; 
     this.img_path = img_path; 
} 

Recipe.prototype.getInfo = function() { 
    return { 
     name: this.name, 
     price: this.price, 
     details: this.details, 
     img_path: this.img_path 
    } 
} 

var recipe1 = new Recipe("tomatonoodles", 7.99, "without gluten", "img/tomato.jpg"); 
var info = recipe1.getInfo(); 

alert(info); 
+0

두 답변을 정확한 것으로 특성화 할 수 있다면 고맙습니다. – Skemelio

+0

이 답변이 도움이 되었다면, 당신은 그것을 upvote 수 있습니다! – Sun

+0

물론 @Peter Leger! – Skemelio

1

이 더 작동 방법.

const newRecipe = (name, price, details, img_path) => { 
    const obj = {}; 
    obj.name = name; 
    obj.price = price; 
    obj.details = details; 
    obj.img_path = img_path; 
    return obj; 
} 

const recipe1 = newRecipe('pasta', 1000, 'something', 'img/pasta.jpg'); 
console.log(recipe1); 
관련 문제