-2

I have this code:

var obj=new function(){
  this.var=null;
  this.fun=function(funVar){
    console.log('funVar: ',funVar);
  }
  console.log('init',this);
  this.fun('fun');
};
obj.var='Something';
obj;

In the console log I have var==null and also var=='something': enter image description here

I don't understand what is happening.

I was hoping to do something like obj; to initiate the object. I know how to do obj={init:function(){}} then use obj.init(), I was just experimenting and now I'm curios.

Alqin
  • 1,305
  • 3
  • 16
  • 35
  • 3
    `obj;` by itself can’t initiate anything, because it’s just an object, not a function call. `new function(){};` constructs an object from an anonymous function. It’s like `new SomeConstructor();` or the equivalent `new SomeConstructor;`, but with an anonymous function. – Sebastian Simon Mar 13 '17 at 14:59
  • do you know anything about `var xxx = function() { ... }`; – Vishal Kumar Sahu Mar 13 '17 at 14:59
  • Someone answer then delete it afterwards that said is about the scope of `var`, there are actually two `var` variables the `obj.var='something'` and the one inside the function. – Alqin Mar 13 '17 at 15:13
  • I'm not sure what your question is. – Phrogz Mar 13 '17 at 18:10

1 Answers1

0

In short:

  1. Functions are first-class values in JavaScript, just like objects, arrays, etc. So, you can write var foo = function(){}; which will create a new function value, and place a reference to that value in the variable foo. As with any function, you must put parentheses after the reference to invoke the function:

    var a = function(){ console.log('hi') };
    var b = function(){ console.log('bye') };
    var c = a;
    a;   // does nothing useful. similar to `42;` or `true;` as a statement
    a(); // "hi"
    c(); // "hi"
    b(); // "bye"
    
    console.log(typeof a); // "function"
    
    console.log(a==b); // false
    console.log(a==c); // true
    
    b = a;
    b(); // "hi"
    console.log(a==b); // true
    
  2. Functions values can have custom properties placed on them, like JavaScript objects. For example:

    function bar(){
      // arguments.callee gives the function value being called
      arguments.callee.callCount++; 
    }
    bar.callCount=0; // I just made up a new property!
    bar();
    bar();
    bar();
    console.log( bar.callCount ) // 3
    
  3. When you use a function as a constructor in JavaScript by using the new keyword, the runtime (1) allocates a new object, (2) runs the function with this new objects as the this scope, and (3) returns that new object (not whatever the function returned).

    So: var x = new bar(); creates a new empty object, invokes the bar function with that object as the this scope, and then puts a reference to that object into the variable x. Note that this new object is different from the function value. You can have a property with the same name, but different values, on different objects:

    function jim(){
      this.jam = "my jam";
    }
    jim.jam = "shared jam"; // a custom property on the function
    
    var jm1 = new jim();
    var jm2 = new jim();
    console.log(jm1.jam); // my jam
    console.log(jm2.jam); // my jam
    console.log(jim.jam); // shared jam
    
    jm2.jam = "special jam";
    console.log(jm1.jam); // my jam
    console.log(jm2.jam); // special jam
    console.log(jim.jam); // shared jam
    
Phrogz
  • 296,393
  • 112
  • 651
  • 745