JS object syntax

var pizza ={
crust:'thin',
var toppings = 3;
};

some times i see "double quotes " and other times i see 'single quotes ’ being used for the value
which is most preferred of the 2?
is crust:‘thin’ public or priavte object
and
is var toppings public or priavte object

Both are acceptable, but try not to mix & match. Personally, I use single quotes in JS and double quotes in HTML. And double quotes in DOM strings within JS.

crust:'thin' is not an object - it’s a property of the pizza object.

Is var toppings = 3; valid property (key : value) syntax for an object?

2 Likes

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

The value is a string, and whether you use single or double quotes is a matter of style (mostly).

JavaScript does not have any access modifiers like you’d have in some other languages. crust is accessible so long as pizza is in scope. That is, it would be if pizza were a valid object. You can’t use variable declaration syntax inside of object literals

var pizza ={
   crust:'thin',
   var toppings = 3; // <-- syntax error
};

var pizza ={
crust:'thin',
toppings: 3 // <-- the only valid syntax for object literals
};
1 Like

thanks for the heads up
but
how do i make a private object inside pizza variable?

I seem to recall some of the JSON API and Ajax lessons failing with single quotes. The same code passed when I switched to double quotes in the FCC code editor. I have not gone back to experiment with this, nor have I checked with other browsers, etc.

jQuery’s style guide prefers double quotes:
http://contribute.jquery.org/style-guide/js/#quotes

Encapsulation can be accomplished in JavaScript with closures. Instead of creating an object with the object literal syntax, return it from a function. Define a getter and/or setter for the value if you want.

var pizza = function(toppings) {
    var toppings = toppings;
    
    return {
        crust: 'thin',
        toppings: function() { return toppings; },
        makePizza: function() { return "A " + this.crust + " crust with " + toppings + " toppings"; },
        addTopping: function() { toppings++;},
        removeTopping: function() { toppings--;}
};

var myPizza = pizza(3);

Toppings cannot be directly modified this way.

2 Likes

I think you could use the revealing module pattern.

var pizzaModule = function () {

  var toppings: [
    "sauce",
    "cheese",
    "fishheads"
  ];

  // private methods
  function bakePizza() {
    // logic
  }

  // public methods
  function makePizza() {
    // logic
  }

  // 'export' public methods
  return {
    makePizza: makePizza
  };

}

More here:

2 Likes

good video u sent me
but i feel its diviating from what im learning right now
or maybe its just a little too complicated for me right now
im watching this vid

I only watched the first ~18 minutes, but around 12:30 the author makes a constructor function. That’s different than the object literal syntax in your original post.

http://www.w3schools.com/js/js_object_definition.asp

Properties defined within a constructor are not globally accessible. New objects created from the constructor inherit those properties; they are accessible properties of the new objects.

1 Like