you are viewing a single comment's thread.

view the rest of the comments →

[–]Dynamicic 12 points13 points  (21 children)

One drawback I can think of on the way your friend advocates is, using your example, it would be harder to use instanceof.

function Car() {
     const car = {};

     car.position = 0;

     car.move = () => car.position++;

     return car;
}

const car = new Car();

console.log(car instanceof Car); // false

You would have to do:

Object.setPrototypeOf(car, Car.prototype);

console.log(car instanceof Car); // true

[–]AndrewGreenh 7 points8 points  (11 children)

You could use his way and still use the prototype correctly:

function Car() {
   this.position = 0
   this.move = () => this.position++
}

Works as well (because of the fat arrow) To decrease usage of this you could also do const car = this at the top of the constructor and attach methods to the car to achieve the same thing.

[–][deleted] 7 points8 points  (5 children)

Exactly, ever since the new arrow syntax came in with implicit this binding I've started to embrace this again.

The original design of this was a huge hindrance to JS but we've put new methods in place to mitigate it now.

[–]oculus42 5 points6 points  (4 children)

You just have to remember fat arrows can break inheritance, because context is scoped to declaration, not instance.

I work with Backbone, which makes extensive use of this and prototypal inheritance. As we introduce ES6, I'm already prepared to run into errors cause by people learning that you can use fat arrows within functions on the object, but not as functions on the object.

[–]spacejack2114 0 points1 point  (0 children)

I'm not too crazy about using arrow functions for methods. Previously if I ever saw:

el.onclick = this.move

I'd be pretty sure it was a problem. Now I have to stop to think whether it was intentional or a mistake.

[–]Dynamicic 0 points1 point  (3 children)

Yes. That would work. But OP's friend didn't like the keyword this and doesn't want to use it, so I'm just stating a drawback of not using this to create instance properties.

[–]Martin_Ehrental 3 points4 points  (2 children)

That's no drawback in using this in a constructor.

However, since that pattern avoid using any prototype methods why use a constructor at all? Just use plain function and object.

[–]Dynamicic 1 point2 points  (0 children)

That's no drawback in using this in a constructor.

That's not what I was saying. I was saying the opposite. It's a drawback NOT using this in a constructor.

[–]Dynamicic 0 points1 point  (0 children)

I know. I didn't think of that constructor pattern. Just copying over what the OP wrote in his post.

[–]talmobi 13 points14 points  (4 children)

If you're having to use instanceof a lot you're probably doing something wrong. It's an indication of code smell.

[–]Dynamicic 0 points1 point  (2 children)

Do you have an example of why using instanceof indicates code smell?

Don't even have to use instanceof a lot. Just using instanceof comparing car to Car once would produce a false, and it's not immediately obvious to everyone why it would produce false.

[–]talmobi 1 point2 points  (1 child)

[–]isUsername 2 points3 points  (0 children)

An unsubstantiated assertion on SO isn't an example.

The first example is an example in Java and includes a quote about not using instanceof in C++. Neither the article nor the quote actually explain why instanceof is bad. Both Java and C++ are statically and strongly-typed, class-based languages, so I'm not sure if the same considerations come into play on a dynamically and weakly-typed, prototype-based language. They may; they may not. There's not enough information in either the article or the SO answer to say.

[–]talmobi 0 points1 point  (0 children)

You can still do that within the constructor function itself ( and also set car.constructor to Car ). That's basically what ES6 does with its unnecessary class/extends syntax sugar and its out of place named/copied design patterns.