Questions about objects and prototypes in javascript?

var obj={
    name:"obj",
    size:0,
    describe:function (){
        return "nothing";
    }
};
undefined
typeof obj.prototype;
"undefined"
//"undefined"
........................................................................
function Person(name){
    this.name=name;
}
undefined
Person.prototype.describe=function (){
    return "Person named: "+this.name;
};
 (){
    return "Person named: "+this.name;
}
var a=new Person("a");
undefined
Person.prototype instanceof Object;
true
typeof Person.prototype;
"object"
//"object"

Person.prototype===Object.create(Person.prototype);
false
//false
The

question inserts the code, why the first typeof is "undefined", the second is "object", and
why the last place is false

Mar.29,2021

question 1. Prototype / Why here is "undefined"
answer: only the method has the prototype attribute, and the obj you define is an object, only the _ _ proto__ attribute. If you print the obj.__proto__, the result is not undefined.
question 2.Unip / Why here is "object"
answer: because your Person is a method, Have prototype attribute
question 3 / / Why is it false
answer: check Object.create 's api,Object.create (proto, [propertiesObject]), where the parameter proto is the prototype object of the newly created object
, so obj.prototype = = Object.create (obj.prototype). _ _ proto__;
of course you can also print out the two and see the difference.
attach a link reference: JavaScript's in-depth chain from prototype to prototype


  1. Why is the first typeof "undefined"?
    obj is an instance object, and there is no prototype by default
  2. Why is the prototype in the second place object?
    the second place you are using a funtion,function with prototype, by default and only one constructor attribute pointing to itself by default.
  3. Why is the last place false??
    Object.create creates a new object, implementation similar to the following

      Object.create =  function (o) {
          var F = function () {};
          F.prototype = o;
          return new F();
      };

1, the prototype of an object is equal to the prototype; of its constructor
2, Object.create (Person.prototype) is created with Person.prototype as the prototype object, which is definitely not equal to Person.prototype


you printed the prototype attribute of obj, the object does not have this attribute, only the function has it, so it will be undefined , and the second place where you print typeof Person.prototype Person.prototype should be a function, typeof function result is object, last do not say.


needs to distinguish between the way the constructor gets the prototype from the object that comes out of new: the constructor directly sets or gets the prototype using prototype, and the object gets

through prop or Object.getPrototypeOf.

first, obj itself does not define the property prototype
second, the prototype is the object
third, one object is not equal to another object, Object.create (Person.prototype) uses Persion.prototype as the prototype to create an object

Menu