Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function and function in javascript

    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
    Last edited by jigneshjsoni; 03-20-2012 at 08:20 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    But there are the "Function" and "Event" objects, used like
    Code:
    var myfunc = new Function(.....);
    var myevt = new Event(...);

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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:

    Code:
    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.

    Code:
    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.

    Code:
    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 [b]new Function[/code] and cannot be coded using function functionName() at all.

    Code:
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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/tutoria...vascriptobject

    Thanks

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by devnull69 View Post
    But there are the "Function" and "Event" objects, used like
    Code:
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by jigneshjsoni View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    randy2gray (03-21-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •