-1

I have written the exact code in console and the problem which I am facing with this code is:

  1. why is xyz.c is defined
  2. why abc.count is undefined

code:

function setup()
{
   this.count = 0;
   return function() 
    {
       this.c = 1;
       return this.c;
    };
};
var abc = new setup();
abc.count;
//undefined 
var xyz = new abc();
xyz.c;
//1
kingshuk basak
  • 423
  • 2
  • 8
  • sorry for that, now i have edited my code.please check it @RGraham – kingshuk basak Aug 12 '15 at 08:25
  • `new` should only be used with constructors. Constructors don't return functions. `setup` is not a constructor. – The111 Aug 12 '15 at 08:54
  • possible duplicate of [What is returned from a constructor?](http://stackoverflow.com/questions/3350215/what-is-returned-from-a-constructor) – Hacketo Aug 12 '15 at 08:58

4 Answers4

2

From the MDN docs on the new operator:

The object returned by the constructor function becomes the result of the whole new expression*. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)

*Emphasis mine

What you're doing here is overriding the default behavior of new. Your code equates to the following:

var abc = function() 
{
    this.c = 1;
    return this.c;
};
abc.c; // undefined. 

Even though you've declared this.c your function() hasn't actually been executed at this stage, so this.c is still undefined.

Then, you new abc with the traditional approach, so:

var xyz = new function() {
    this.c = 1;
    return this.c;
};
xyz.c === 1; // Works, because you've new'd the function correctly

Assuming you're trying to implement a counter object-oriented style function, you could try something like:

function setup()
{
   this.count = 0;
   this.increment = function() {
       this.count++;
   }
};
var counter = new setup();
counter.increment();
counter.increment();
console.log(counter.count); // 2
var counter2 = new setup();
counter2.increment();
console.log(counter2.count); // 1

Or using prototypal inheritance:

function setup() {}
setup.prototype.count = 0;
setup.prototype.increment = function() {
    this.count++;
};

Although naming convention would dictate you should call your class something like:

function Counter() {
    this.value = 0;
    this.increment = function();
}
var first = new Counter();
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
0

typeof abc is "function" which should be object to access abc.count. I get this error on executing "var xyz = new abc();"

VM40:7 Uncaught ReferenceError: count is not defined at new (:7:17) at :2:11 at Object.InjectedScript._evaluateOn (:905:140) at Object.InjectedScript._evaluateAndWrap (:838:34) at Object.InjectedScript.evaluate (:694:21)

which is not correct.i wonder how you are getting 3?

0

JavaScript does prototypical heritage. You are trying to call a constructor for a function which in turn returns a function.

Try console.log(setup) to see what it contains. It's not an object, it has no count property, it's a function. An when you call it via setup()() it will always return 1.

What you want could work like this:

var Counter = {
    count: 0,
    increaseCount: function() {
         return ++this.count;        
    }
}

var abc = Object.create(Counter);
abc.increaseCount(); // 1
abc.increaseCount(); // 2
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
0

You can see typeof xyz is object ,hence it is accessing the property of the function. and typeof abc is a function. you cannot access a property with function.