JavaScript Prototype

Asked 1 years ago, Updated 1 years ago, 33 views

function person(name){
  this.name = name;
  This.prof=function(){
    console.log("name:"+this.name+"\nage:"+this.age+"\ncolor:"+this.color+"\nweight:"+this.weight);
  }
}
person.prototype.color="red";
person.prototype.age="18";

varman = new person("Jackbow");
man.weight = 50;
man.prof();

This man.weight=50; is

man={
  weight —50
};

Why shouldn't I?

javascript

2022-09-30 21:02

1 Answers

{
  weight —50
}

This statement creates an object with only the weight property.This object has nothing to do with man or person.So,

man={
  weight —50
};

The new object replaces the entire man content.

man.weight=50;

If you write , only the weight property changes, so it does not affect other properties or the prototype chain.


2022-09-30 21:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.