title: Javascript: Function, Object, new ....a little bit of confusion
body: <p>in web I read: </p>
  
  <blockquote>
    <p>"Well, Javascript, or better ECMAScript, is an Object Based Language so..
    Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript where an Object is a "bag" of properties and methods..."</p>
  </blockquote>
  
  <p>and:</p>
  
  <blockquote>
    <p>When a function is invoked with “new” keyword then the function is known as constructor function and returns a new instance. In such cases, the value of “this” refers to newly created instance.</p>
  </blockquote>
  
  <p>Now, my code:</p>
  
  <pre><code>function fe(){
      var i;
      var j = 0;
      return j;
  };
  process.stdout.write("fe --&gt; "); console.log(fe);      // [Function: fe]
  process.stdout.write("fe() --&gt; "); console.log(fe());  // 0
  
  var vfe = new fe();
  process.stdout.write("vfe --&gt; " + vfe + " - "); console.log(vfe);      [object Object] - fd {}
  
  
  
  
  var fd = function(){
      var i;
      var j = 0;
  };
  process.stdout.write("fd --&gt; "); console.log(fd);      // [Function: fd]
  process.stdout.write("fd() --&gt; "); console.log(fd());  // undefined
  
  var vfd = new fd();
  process.stdout.write("vfd --&gt; " + vfd + " - "); console.log(vfd);      [object Object] - fd {}
  </code></pre>
  
  <p>In the first I have value returned and new return an object as the last case: I have an object {} and so I could append properties and methods </p>
  
  <p>in an another case:</p>
  
  <pre><code>function fe(){
      var i;
      var j = 0;
      return fe;
  };
  process.stdout.write("fe --&gt; "); console.log(fe);        // [Function: fe]
  process.stdout.write("fe() --&gt; "); console.log(fe());    // [Function: fe]
  
  var vfe = new fe();
  process.stdout.write("vfe --&gt; "); console.log(vfe);      // [Function: fe]
  
  
  var fd = function(){
      var i;
      var j = 0;
      return fd;
  };
  process.stdout.write("fd --&gt; "); console.log(fd);      // [Function: fd]
  process.stdout.write("fd() --&gt; "); console.log(fd());  // [Function: fd]
  
  var vfd = new fd();
  process.stdout.write("vfd --&gt; "); console.log(vfd);    // [Function: fd] 
  </code></pre>
  
  <p>In this case I have a value or a Function returned that is all but an object....
  Now:
  id the "Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript where an Object is a "bag" of properties and methods..." not so true in all situation?</p>
  
  <p>Another situation is the following:</p>
  
  <pre><code>function fe () {
      var pri_var1;
      // private, not accessible from extern
      function pri_fun1(){ return "pri_fun1"; }    
      // private, not accessible from extern
      function pri_fun2(){ return "pri_fun2"; }       
      //static property
      fe.sta_var1 = 0;    //static property ....is never a good idea but...for folks!!!
      //static method
      fe.sta_fun1 = function(){return pri_fun2();}
      //public not heritable
      this.pub_fun1 = function(){return "pub_fun1";}
      //public heritable
      fe.prototype.pub_her_fun1 = function() { return "pub_her_fun1";}
      //return fe;
  }
  
  var obj2 = new fe();
  console.log("obj2 --&gt; ",obj2);
  console.log(obj2.pub_fun1());
  console.log(fe.sta_fun1());
  </code></pre>
  
  <p>If I uncomment //return fe; the "node" say:
  console.log(obj2.pub_fun1());
                   ^</p>
  
  <p>TypeError: obj2.pub_fun1 is not a function...
  If I leave the comment all works as well....</p>
  
  <p>A little bit of confusion about Function and Object in (again):</p>
  
  <blockquote>
    <p>"Well, Javascript, or better ECMAScript, is an Object Based Language so.. Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript where an Object is a "bag" of properties and methods..."</p>
  </blockquote>
  
  <p>thanks in advance </p>
  
  <p>Ok...I'll try to be more precise.
  I read "Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript where an Object is a "bag" of properties and methods..."
  Hence a function is an object? Every function?
  Take the function:</p>
  
  <pre><code>function fe(){
      return 1;
  };
  </code></pre>
  
  <p>and the following instructions:</p>
  
  <pre><code>console.log(fe);    // [Function: fe]
  console.log(fe());  // 1
  var vfe = new fe(); 
  console.log(vfe);   // fe {}
  </code></pre>
  
  <p>if I read well:
  fe is a function that, being non primitive, is an "object" (what kind of object? Maybe somewhat complex type representation [Function: fe] called improperly object in a not OO way?)... but it becomes object in OO way of "bag" {} only after a new (vfe)...., but it can be also be invoked (fe()) and produce a value (1)...or can return itself or by default undefined...
  Some functions could not be executed but only "instantiated"? and other function can contains functions which are classical function to execute to get a normal value but not to be instantiated??? 
  Very, very strange behaviour...
  Can I have an help in terms of naming, design and boundaries about this mess?
  thanks again... </p>