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.

8
  • 1
    Car.makeCar is a function not a Car object. Commented Aug 31, 2016 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. Commented Aug 31, 2016 at 23:14
  • bmw instanceof Car does still work, because you're still creating the instance to inherit from Car.prototype Commented Aug 31, 2016 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". Commented Aug 31, 2016 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. Commented Aug 31, 2016 at 23:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Whoa, insta-accept in exactly 7 seconds! That's hard to beat :-)

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.