Js prototype
function People(name){
this.name=name;
//
this.Introduce=function(){
console.log("My name is "+this.name);
}
}
//
People.Run=function(){
console.log("I can run");
}
//
People.prototype.IntroduceChinese=function(){
console.log(""+this.name);
}
//
var p1=new People("");
p1.Introduce();
People.Run();
p1.IntroduceChinese();
recently I am learning the prototype of js, the example of searching on the Internet, what is the difference between object method and class method and prototype method? How to choose in the process of practical application?
Hello, first of all, you have to understand what new
has done
goes deep into object-oriented and prototyping. re-recognize new]
after reading this article, you should be able to understand that
object method
is a [constructor] bound to the instance of constructor People
. You can understand that
class method
is bound to the constructor People itself.
prototype method
is bound to the prototype attribute
of constructor People. The purpose is to make the instance of the constructor People
be able to call the prototype method of the constructor People through its own _ _ proto__ attribute
.
if you don't have a thorough understanding of the concept of class / instance
, I suggest you read this article I wrote
deep into object-oriented and prototyping [Concepts 1]
digression
I guess privately how you should choose in the process of practical application.
this problem is because you don't know anything about object-oriented programming
this proposition is too big. If you are free, you can read the blog I have written. I hope it will be helpful to you.
goes deep into object-oriented and prototyping [concept 2]
goes deep into object-oriented and prototyping [concept 3]-- Prototyping chain and inheritance
The
object method is owned only by the current object, and modifying it does not affect other objects of the same class.
the prototype method is common to all objects of the same class, and the modification affects all objects of that class.
class methods are static methods of this class, only through the class name. The method name ()
is called in this way, not through the object. Called in the form of method name ()
. And the this
when the class method is called is not the same as the object method. The this
of the former is the constructor of the class, while the this
of the latter is the current object of the class.