Skemelio Skemelio - 11 months ago 32
Javascript Question

Javascript: How to store an object in a reference

I am new to Javascript and I am getting highly confused.

I created an object,

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

and I created a function that it's something like a constructor. It gives values to the object's properties,

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

And then I need to pass recipe object to a reference,

var recipe2 = recipe;

Is that correct?


objects are reference types and your class definition should follow javascript language constructs and conventions (i.e use pascal case for classes)

go here and learn


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


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

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