1

New to javascript. Let's say I have a constructor like this:

function Dependent(dependency) {
    this.doSomething = function(x) {
       dependency.doSomethingReal(x);
    }
}

var impl = new SomeImplementation();
var dependent = new Dependent(impl);
console.log(dependent.doSomething(3));

My understanding is that there is nothing in the language that can help to ensure that impl can in fact fulfill its responsibilities (actually has a method called doSomethingReal that takes an argument).

A few questions come up:

  1. In the constructor-function should I manually check the dependency argument to ensure that it has all the things Dependent requires?
  2. Should I just not worry about it?
  3. How do the other libraries deal with this situation? For example, I know there are a couple DI projects...or MVC projects that for example require their view objects to implement certain well-known-methods.

I realize that I can just pass a function into the constructor. In other words, if dependency was a function then we'd just invoke it. Is that the safest way to do it? I don't think that's what the MVC projects do...also there are times that it makes sense to pass in an object.

2
  • you can turn the function into a string and RegExp the formal parameters into an array, you can then do whatever you need with that array, but it's tricky to get arbitrary names w/o eval, but if you have a certain collection ahead of time, it's easy to name members as formal properties. then, you have to either recompile the function or use a wrapper to alter the arguments as call-time. i would use a wrapper, which you have a start of as Dependent() Commented Jul 15, 2015 at 20:36
  • It really depends on what the side affects are going to be of having the dependency not implement the requirement. Often major libraries just fail silently, but then again, it just depends on what happens as the result. If the side affect is that nothing happens, then fail silently. If the side affect is that your player suddenly gets 0 hit points, then perhaps that should be handled with some sort of workaround or thrown error to catch elsewhere. Commented Jul 15, 2015 at 20:48

2 Answers 2

1

You can use instanceof to check if an object is an instance of another one.

For example, within your code:

function Dependent(dependency) {

    // here we could check that dependency is an instance of SomeImplementation
    if (!(dependency instanceof SomeImplementation))
        throw "dependency must be an instance of SomeImplementation";

    this.doSomething = function(x) {
        dependency.doSomethingReal(x);
    }
}

var impl = new SomeImplementation();
var dependent = new Dependent(impl);
console.log(dependent.doSomething(3));

In javascript it's also common to use the 'duck typing' method to validate an object. For example:

console.log (
    'isABird' in duck &&
    'walks' in duck &&
    'swims' in duck &&
    'quacks' in duck ?
    "juhm... I'm pretty sure we're dealing with a duck" :
    "meh... since I a expect a duck to be a bird, walks, swims and quacks, then this buddy is definitely not a duck"
);
Sign up to request clarification or add additional context in comments.

1 Comment

There's rarely a reason to use instanceof, however, much like there's no real reason to the corresponding version in C#/Java. Use duck-typing instead OP
0

Well, as far as I have understood it, Duck Typing would be the natural way to deal with this problem in JavaScript since JavaScript is not a strict typed language.

In consequence this would mean that you indeed just accept, that JavaScript is loosely typed and that you will have to deal with runtime-errors when you try to access a method on an object that does not have this method. (Your option 2)

Apart from that, you could use a pattern that tries to simulate interfaces or abstract classes in JavaScript which works like you have suggested in option 1 and which is described here in detail:

http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#decoratorpatternjavascript (Chapter "Pseudo-classical Decorators")

But this would also just lead to runtime-errors. The exceptions might just rise up a little earlier but not at "compile time". So in both designs you will need to test your application in order to find type-related-errors.

So I tent to accept that Duck Typing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.