0

I've recently discovered prototypes for myself, and would be happy to get some help with figuring out the thing:

Code example 1:

    "use strict";

    var Car = function(loc) {
        var obj = Object.create(Car.prototype);
        obj.loc = loc;
        return obj;
    };

    Car.prototype.move = function() {
            this.loc++;
            return this.loc;
    };

    var bmw = Car(6);

    console.log( bmw instanceof Car );

Code example 2:

    "use strict";

    var Car = {
        "makeCar": function(loc) {
            var obj = Object.create(Car.prototype);
            obj.loc = loc;
            return obj;
        },
        "prototype": {
            "move": function() {
                this.loc++;
                return this.loc;
            }
        }
    };

    var bmw = Car.makeCar(6);

    console.log( bmw instanceof Car.makeCar );

I wrote example 2, cause I want to keep methods for Car object inside the object itself. I'm not sure if it can be done, but it's working so far, except the "instanceof" operator.

In the first example it will log "true", but in second it will log "false" and I've no idea where am I wrong, so counting on your help, thank you.

Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
Mike
  • 1,979
  • 2
  • 16
  • 29
  • 1
    `Car.makeCar` is a _function_ not a _Car_ object. – VLAZ Aug 31 '16 at 23:12
  • `instanceof` tests whether the object's prototype chain contains the prototype of the constructor. `bmw`'s prototype chain contains `Car`, not `Car.makeCar`. – Barmar Aug 31 '16 at 23:14
  • `bmw instanceof Car` does still work, because you're still creating the instance to inherit from `Car.prototype` – Bergi Aug 31 '16 at 23:16
  • My initial test for example 2 was console.log( bmw instanceof Car ); but in this case I've an error: "Uncaught TypeError: Right-hand side of 'instanceof' is not callable". – Mike Aug 31 '16 at 23:16
  • 1
    @Mike: Oh, right, `Car` would need to be a constructor for that to work. `Car.makeCar` is not a constructor and does not have the instances' prototype on its `.prototype` property, so that doesn't work either. – Bergi Aug 31 '16 at 23:18
  • @Bergi so there is no way to have methods merged with constructor itself? – Mike Aug 31 '16 at 23:19
  • You can make it work by setting `Car.makeCar.prototype = Car.prototype`, but that's weird. Just keep the standard patterns. If you want to declare your methods in a structural unit together with the constructor, wrap everything in an IEFE [like here](http://stackoverflow.com/a/28256166/1048572) or just use an ES6 `class`. – Bergi Aug 31 '16 at 23:19
  • @Bergi oh, ok thank you, please combine this into an answer, I will accept it. – Mike Aug 31 '16 at 23:20

1 Answers1

1

console.log( bmw instanceof Car.makeCar ); yields false

…because Car.makeCar does not have the instances' prototype on its .prototype property. The instanceof expression is equivalent to Car.makeCar.prototype.isPrototypeOf(bmw). To make it work, you'd need to set

Car.makeCar.prototype = Car.prototype;

but that's weird. Just keep the standard patterns. If you want to declare your methods in a structural unit together with the constructor, wrap everything in an IEFE like here or just use an ES6 class

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375