-2

I'm trying to throw a error as a object, so that I can create a if-statement to check if a error is a emergency-error or not. For this I need to check if the error instanceof myEmergencyFunc. But this fails if I have a sub-function. See this fiddle as a example: http://jsfiddle.net/tvs4qjgs/

var someName = function(){
    this.LogEmergency = function(message){
        this.message = message;
        return this;
    };
    return this;
};

var a = someName().LogEmergency('my error');

console.log(a instanceof someName().LogEmergency);

What am I doing wrong?

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
user3631654
  • 1,735
  • 6
  • 22
  • 38
  • 2
    read more about constructor function in JS and using of "new". – kpblc Aug 19 '14 at 08:26
  • 1
    creating methods in a constructor is considered bad practice. Avoid this type of constructor as much as possible: that's what the prototype is for! Also consider following coding standards/conventions: constructors start with an UpperCase, properties and methods don't (all names are camleCased) – Elias Van Ootegem Aug 19 '14 at 10:52

1 Answers1

1

Problem

var a = someName().LogEmergency('my error');

a is referring to the global object and not the object you think you have created(which is window if you are running this code in a browser)

console.log(a === window) --> will be true.

your final result is wrong because you are comparing with the wrong object.if you are wondering why, that's because you were missing the keyword new while creating the object. calling a function with new triggers the constructor mechanism that creates a new object and returns it.

calling a function without new and returning "this" inside a function returns the global object.

you have to make the following changes to your code

var someName = function(){
  this.LogEmergency = function(message){
    this.message = message;
    return this;  // here this refers to the new object you created
  };
  return this; // here also this refers to the new object you created
               // here the return is redundant as this is implicit.
};

// new operator is the keyword for creating objects.
// the meaning of "this" inside the function completely changes without the operator
var a = new someName().LogEmergency('my error'); 

in the above code a now refers to the new instance you created. finally check if the created object is an instance of someone

console.log(a instanceof someName); //will be true

Read more about constructors here

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
  • You don't need parens around `new someName()`? – Lightness Races in Orbit Aug 19 '14 at 09:14
  • () is optional if you are not passing any parameters. new someName also will work – Prabhu Murthy Aug 19 '14 at 09:16
  • No, you misunderstood me. I'm asking whether you need to write `(new someName()).LogEmergency('my error')` in order to ensure that `LogEmergency` is invoked on your new object, rather than being invoked on the result of calling `someName()` and then itself passed to `new`. – Lightness Races in Orbit Aug 19 '14 at 09:25
  • @LightnessRacesinOrbit: Indeed not, though I would strongly recommend using them, too: `LogEmergency`, to me, looks like a constructor. The method itself uses `this`, so `var a = new someName(); var b = new a.LogEmergency('foobar');` is a dangerous game to play – Elias Van Ootegem Aug 19 '14 at 10:56
  • @BOSS: The invokation brackets in `new someName().LogEmergency()` are _not_ optional. `var a = new someName.LogEmergency();` will throw a TypeError. Either use `var a = (new someName).LogEmergency()` or the code you have, I'd prefer the version Lightness Races in Orbit suggested, thgouh – Elias Van Ootegem Aug 19 '14 at 10:59