Skip to main content
Removed a minor ambiguity
Source Link
hugomg
  • 70.4k
  • 30
  • 168
  • 257

You can't use the object literal notation to set arbritrary properties as you seem to try in the second example. This doesn't stop you from setting the property manualy:

function make_menu_item(name, func){
    var item = {};     //Create an empty object
    item[name] = func; //Assign the property with the name you choose
                       //(obj.nameobj['option1'] andis obj['name']equivalent areto equivalentobj.option1 in Javascript)
    return item;
}

var menu = [
     make_menu_item('option1', function () {...}),
     make_menu_item('option2', function () {...})]
   

You can't use the object literal notation to set arbritrary properties as you seem to try in the second example. This doesn't stop you from setting the property manualy:

function make_menu_item(name, func){
    var item = {};     //Create an empty object
    item[name] = func; //Assign the property with the name you choose
                       //(obj.name and obj['name'] are equivalent in Javascript)
    return item;
}

var menu = [
     make_menu_item('option1', function () {...}),
     make_menu_item('option2', function () {...})]
   

You can't use the object literal notation to set arbritrary properties as you seem to try in the second example. This doesn't stop you from setting the property manualy:

function make_menu_item(name, func){
    var item = {};     //Create an empty object
    item[name] = func; //Assign the property with the name you choose
                       //(obj['option1'] is equivalent to obj.option1 in Javascript)
    return item;
}

var menu = [
     make_menu_item('option1', function () {...}),
     make_menu_item('option2', function () {...})]
  
Source Link
hugomg
  • 70.4k
  • 30
  • 168
  • 257

You can't use the object literal notation to set arbritrary properties as you seem to try in the second example. This doesn't stop you from setting the property manualy:

function make_menu_item(name, func){
    var item = {};     //Create an empty object
    item[name] = func; //Assign the property with the name you choose
                       //(obj.name and obj['name'] are equivalent in Javascript)
    return item;
}

var menu = [
     make_menu_item('option1', function () {...}),
     make_menu_item('option2', function () {...})]