isPrototypeOf in Javascript

前端 未结 3 1091
星月不相逢
星月不相逢 2021-02-20 11:02

I am a beginner to JavaScript and on my way to Prototypes in JavaScript.
As per the article here

Creating

3条回答
  •  盖世英雄少女心
    2021-02-20 11:34

    What you are saying,

    The ‘prototype’ property points to the object that will be assigned as the prototype of instances created with that function when using ‘new’.

    Doesn't make much sense to me, but I think you've got the right idea.

    To me, at least,
    The constructor function is the prototype for your person objects.
    is wrong.

    The correct version would be:
    The constructor function is the constructor function for your person objects.


    The prototype property of your constructor function is an object.

    It contains properties which are assigned to objects instantiated with that constructor function, example:

    function Person(name){
        this.name = name;
    }
    Person.prototype = {
        species: "Human"
    };
    

    Sets up a constructor function with a prototype property, containing a property species.

    Now, if we do this:

    var joe = new Person("Joe");
    

    joe is an object which looks like

    {
        name:    "Joe",
        species: "Human"
    }
    

    As you can see, the properties of the prototype of Person() were set as normal properties of Joe.

    TL;DR

    So I think you had the right idea.

提交回复
热议问题