question

James N. Perdue avatar image
James N. Perdue asked

Newbie Question about OOP

Is there an example working with Lambda using non-object oriented Javascript?
alexa skills kitvoice-user interface
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Robert Hess avatar image
Robert Hess answered
Not being sure exactly what your need/issue is, I'm assuming that you may simply not have a good comfort level with OOP? Since the Alexa functionality is dependent on event handlers, it would be difficult to develop an application without any OOP style of functionality (although "event handlers" are not really the same thing as OOP). However, that doesn't mean your application has to fully embrace OOP programming methodologies. The minecraftHelper example is a relatively simple one which doesn't rely heavily on OOP. If you are needing some assistance in getting started, without feeling overwhelmed by OOP, here are some details which may help. I'll use the minecraftHelper sample as reference: AlexaSkill.js: ------------------- You don't need to touch the "AlexaSkill.js" file, this is just to wire up your functionality into the Alexa framework. recipe.js: ------------------------ The file "recipe.js" is defining an array of name/value pairs which provide the text that echo will speak. If you have a skill in mind which will respond with a stock statement when the user asks for a specific thing, then all you have to do is update this file to contain those name/value pairs. For example, if you want to provide a gardening dictionary, and allow the user to say "Alexa, ask gardener about cover crops", then you would edit recipe.js to contain an entry that looks like: module.exports = { "cover crop": "a cover crop is a crop that is grown to protect and enrich the soil instead of leaving it fallow between traditional crops. Buckwheat is a common cover crop that can be used to restore nitrogen to the soil." } index.js: -------------------- Within the "index.js" file, mainly just focus on the "MinecraftHelper.prototype.intentHandlers" section where it defines the "RecipeIntent" function. This is where basically all of the work of this sample is taking place. Then, in "index.js" you can see code similar to: var itemSlot = intent.slots.Item, itemName; if (itemSlot && itemSlot.value){ itemName = itemSlot.value.toLowerCase(); } Here, "itemSlot" will contain the word/phrase uttered by the user. with "itemSlot.value" being the word/phrase itself. So "itemName" will now be that value. It is being converted to lowercase, just to prevent case from being a problem in matching. Then notice the code: var cardTitle = "Recipe for " + itemName, recipe = recipes[itemName], Here, "recipe[itemName]" will use the (lowercase) string of the word/phrase uttered by the user to look up in your array the definition, thus: recipe["cover crop"] will return the definition contained in your recipe.js for that. Thus, without too much work, you can build up a relatively simple skill which will respond with a set of stock phrases based on user input. -Robert
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

James N. Perdue avatar image
James N. Perdue answered
Wow, thanks Robert for taking the time to make the confusion that is the many event handlers seem a bit less unwieldy. I have programmed a reasonable amount in non-OOP javascript, but the OOP and event handlers are not second nature to me. Comments like yours encourage me to keep studying the examples. Maybe it will sink in.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

James N. Perdue avatar image
James N. Perdue answered
Robert thanks for the help. Can you tell me what this line does in the minecraft example. It doesn't look like javascript to me thanks to the commas. var cardTitle = "Recipe for " + itemName, recipe = recipes[itemName], speechOutput, repromptOutput;
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jon avatar image
Jon answered
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Robert B. Hess avatar image
Robert B. Hess answered
Jonathan is correct, it is just variable creation and assignment combined. Consider: var cardTitle, recipe, speechOutput, repromptOutput; Is creating four variables, and: var cardTitle = "Recipe for " + itemName, recipe = recipes[itemName], speechOutput, repromptOutput; Is exactly the same thing, but this time it is initializing two variables at the same time. Note that: recipes[itemName] Is using a text value (itemName) as an array index instead of a numerical value, this is how JavaScript accesses name/value pairs, as is coming from recipes.js -Robert
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.