...

View Full Version : Function and function in javascript



jigneshjsoni
03-20-2012, 07:09 AM
If I use function or Function to create a javascript function, does it make any difference ? Whats the difference between two and in which situation shall I use either ?

Likewise whats the difference between event and Event objects of JavaScript ?

Thanks

Philip M
03-20-2012, 08:21 AM
Javascript is case-sensitive. The keywords are function and event, not capitalised.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

devnull69
03-20-2012, 08:31 AM
But there are the "Function" and "Event" objects, used like


var myfunc = new Function(.....);
var myevt = new Event(...);

felgall
03-20-2012, 08:53 AM
Do NOT use - new Function() as there is nothing that you can do with that one of the three ways of defining functions in JavaScript that can't be done using at least one of the other two ways and using that way both makes the function code much harder to read by embedding it in a string and also requiring that the script run the function code through eval() each time it is referenced in order to convert it into something that can be run.

The most flexible way of defining functions in JavaScript is:


var myfunc = function() {...};

The difference from using new Function is that the parameters move one earlier inside the () and what was the first parameter is placed between the {} as code to run rather than as a string to eval.

These two ways of defining a function can be placed anywhere in the code including places where the third way can't be used.


function myFunc() {...};

That way can't be used when you want to be able to redefine functions dynamically or define them inside of if or switch statements.

So for example the following is the most efficient way to define a cross browser addEvent function in JavaScript (where the originally called function overwrites itself with the version the current browser supports so as to use that for all the subsequent calls without having to retest.


addEvent = function(ob, type, fn) {
if (window.addEventListener)
addEvent = function(ob, type, fn ) {
ob.addEventListener(type, fn, false );
};
else if (document.attachEvent)
addEvent = function(ob, type, fn ) {
var eProp = type + fn;
ob['e'+eProp] = fn;
ob[eProp] = function(){ob['e'+eProp]( window.event );};
ob.attachEvent( 'on'+type, o[eProp]);
};
else return;
addEvent(ob, type, fn);
};

That would be very messy if you tried to code it using new Function[/code] and cannot be coded using [b]function functionName() at all.


addEvent = new Function("if (window.addEventListener) addEvent = new Function(\"ob.addEventListener(type, fn, false );\",ob, type, fn ); else if (document.attachEvent) addEvent = new Function(\"var eProp = type + fn; ob['e'+eProp] = fn; ob[eProp] = new Function(\"ob['e'+eProp]( window.event );\"); ob.attachEvent( 'on'+type, o[eProp]);\"ob, type, fn ) ; else return; addEvent(ob, type, fn);",ob, type, fn);

and I probably have some of the quotes messed up in that attempt to rewrite the code to use new Function since the calls are nested inside one another three levels deep all inside of the one string.

jigneshjsoni
03-20-2012, 08:53 AM
But there are the "Function" and "Event" objects, used like

var myfunc = new Function(.....);
var myevt = new Event(...);
Thats what my question is. Whats difference between Function and functiond AND Event and event ? Which one to use and when ?

Please inform me about the differences.

Please check following link. Event and event are mentioned separately there
http://www.howtocreate.co.uk/tutorials/javascript/javascriptobject

Thanks

Philip M
03-20-2012, 08:54 AM
But there are the "Function" and "Event" objects, used like


var myfunc = new Function(.....);
var myevt = new Event(...);


Ye, that is so. Strictly every function in JavaScript is actually a Function object. To try to answer the question:-

When a Function object is created by using the Function constructor, it is evaluated each time. This is not as efficient as the alternative or normal method of declaring a function using the function statement where the code is compiled, and is not recommended except in special circumstances. Having said that it is very useful in some specific situations.

The following example creates a Function object to calculate the average of two numbers, and then displays that average:

var twoNumAverage = new Function("x", "y", "return (x + y)/2");
Note that as before the word Function is case sensitive.

The Function object can be called just as if it were a function by specifying the variable name:

var average = twoNumAverage(12,17);

For more info see for example:-
http://permadi.com/tutorial/jsFunc/index.html

jigneshjsoni
03-20-2012, 05:43 PM
When a Function object is created by using the Function constructor, it is evaluated each time. what exactly is meant by evaluating a function ?

Thanks

Philip M
03-20-2012, 06:14 PM
what exactly is meant by evaluating a function ?

Thanks

Evaluate means solve, or calculate the answer.

new Function is the equivalent of eval(). The eval() function evaluates or executes an argument (a string). If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

Example:- eval("x=10;y=20;document.write(x*y)"); // output is 200

However, every time that piece of code runs JavaScript will have to revert to interpreter mode, slowing down your code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum