Thursday, November 15, 2012

Different syntax's for defining a function in javascript

I\’m working through a book on javascript, and on one of the examples they are displaying these two blocks of code to be used in the example:



showTipListener: function(event)
{
Tooltips.showTip(this);
Core.preventDefault(event);
}
hideTipListener: function(event)
{
Tooltips.hideTip(this);
}


and



var Tooltips =
{
init: function()
{
var links = document.getElementsByTagName(\"a\");

for (var i = 0; i < links.length; i++)
{
//Every link is going to need a title attribute in the HTML
var title = links[i].getAttribute(\"title\");

if (title && title.length > 0)
{
Core.addEventListener(links[i], \"mouseover\", Tooltips.showTipListener);
Core.addEventListener(links[i], \"focus\", Tooltips.showTipListener);
Core.addEventListener(links[i], \"mouseout\", Tooltips.showTipListener);
Core.addEventListener(links[i], \"blur\", Tooltips.showTipListener);

}
}
}}//I also had to add this extra closing bracket to avoid errors? Don\'t understand why
}


What I\’m worried about is where does the first code sample go in reference to the second code sample?


I\’ve never used this syntax before (as seen in the second code sample) to define a javascript function:



functionName: function(passedParam)
{
//function body
}


The above is just an example, not part of my code.


Maybe these functions are to be inside of the Tooltips object? This was a thought of mine, so I did it, put the two functions inside of the ToolTips object\’s brackets. Now, I get this error in FireBug:

My FireBug error


I\’m just looking for any help and guidance anyone has to offer. I\’m not new to javascript, but am new to defining functions this way.






Rating: 4 out of 5 based on 3 ratings



The post Different syntax's for defining a function in javascript appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/different-syntaxs-for-defining-a-function-in-javascript.html

No comments:

Post a Comment