1

I wanna do the following, if the parameter passed is a contructor, then do new 'constructor' if not, just use the instance. How can I do that?

This is what I've done so far, but it doesn't work. I think something is wrong with my code:

JS

var showList = function (view, options) {

  // checking if view is a conctructor or not
  if (view instanceof view) {
    app.getRegion('main').show(view(options));
  } else {
    app.getRegion('main').show(new view(options));
  }                
}

so the above function can be used as:

var listView = new ListView;
showList(listView);

or straight:

showList(new ListView);
Shaoz
  • 10,573
  • 26
  • 72
  • 100
  • http://stackoverflow.com/a/19717946/2701144 to test if the parameter is a constructor (i.e. a function). – Brett May 19 '14 at 15:30
  • Without even performing the check, the example you used of `showList(new ListView)` will work, and I’d recommend you just do that. It’s nearly as short as `showList(ListView)`, and can save you a lot of pain. – Ry- May 19 '14 at 15:35
  • Thanks @minitech. It's the way the framework has was built, so I just wanted to make this function to be a bit flexible when accepting parameters. – Shaoz May 19 '14 at 16:00

3 Answers3

1

I think you're going to want to test whether the argument is an object or a function:

if (typeof view === "function")

will tell you it's a function (a constructor function in your context)

if (typeof view === "object")

will tell you that it's an already constructed object.


var showScreen = function (view, options) {

  // check if view is already an object
  if (typeof view === "object") {
    app.getRegion('main').show(view(options));
  } else {
    app.getRegion('main').show(new view(options));
  }                
}

One thing I'm confused about in your code is if view is already an object, then why do you do view(options). That doesn't make sense to me. Doing new view(options) when view is a function makes sense, but not the other option so I think something also needs to be corrected with that line of code. Do you perhaps mean to call a method on that object?


FYI, I tend to avoid using instanceof as a general practice if there is another option because instanceof can have issues with cross frame code whereas typeof does not have those issues.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0
var showScreen = function (view, options) {

  // checking if view is a conctructor or not
  if (view instanceof Function) {
    app.getRegion('main').show(new view(options));
  } else {
    app.getRegion('main').show(view(options));
  }                
}

Maybe not the best way but well.

function A(){}
var a = new A();

a instanceof A // true
a instanceof Function // false
A instanceof Function // true
Vincent Thibault
  • 601
  • 5
  • 16
  • This is comparing instances vs functions. OP wants constructor functions vs regular functions. – Halcyon May 19 '14 at 15:43
  • @Halcyon - the OP's code example at the end of their question clearly shows passing an object so that part of the question makes it look like it's object vs. constructor function. – jfriend00 May 19 '14 at 16:04
  • I think you misunderstand. Look at the `view` variable. It is either a function `view(options)` or a constructor `new view(options)`. That he is passing an object is probably a bug, the missing parens are questionable. – Halcyon May 19 '14 at 16:07
0

This seems like a code smell to me. I think it's better pass an instance instead of a constructor function.

In this case you can do:

showScreen(new ListView(options))

If it's hard to construct a ListView you should wonder why that is.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Thanks for your response @Halcyon. The reason why it's coded like that, is because that's how the codebase was written before I touch the code. I asked my question to give me some insights... – Shaoz May 19 '14 at 15:40
  • You could try some black magic involving inheriting from `Function`. – Halcyon May 19 '14 at 15:44