SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    constructor comparison

    Hi,
    sorry to disturb you.

    Code JavaScript:
    var foo = function() {};
    var f = new foo();
    f.constructor;//function
    f.constructor === foo;//true 
    function bar() {};
    var b = new bar();
    b.constructor; //bar()
    b.constructor === bar;//true

    Why
    f.constructor give me function
    b.constructor give me bar()
    what's the difference ?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foo refers to an unnamed anonymous function, bar is a named global function.

  3. #3
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is a function expression
    Code:
    var foo = function() {};
    Is a function declaration
    Code:
    function bar() {};
    You can have a named function expression
    Code:
    var foo = function foo() {};
    Tests
    Code:
    var foo = function foo() {};
    var f = new foo();
    console.log(f.constructor); // foo
    
    var fn = function(){};
    console.log(fn.name); // an empty string
    
    var fn = function fn(){}; 
    console.log(fn.name); // fn

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to everybody.


Bookmarks

Posting Permissions

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