prototype
1、
prototype is associated with Clone.
That is, when an instance is created, prototype clones members to the instance of the Class(function).
Detail: The most common prototypes in the built-in objects, such as: the Array prototype has the join and split method.
When creating array a var a=[1,2], all methods in the prototype are clone onto a.
2. This is an instance pointer of this class (this pointer is "dynamic bind"). How to understand the dynamic linkage of js this, please refer to the article I wrote: /?id=117
When creating an instance of this class, the instance has all predefined with similar members. It also has members defined in the prototype prototype. If the internal definition of the class is the same as one of the definitions in the prototype, it is not a rewrite:
Looking at this example, jsclass defines func defined in prototype. If there are members inside jsclass and the ones in the prototype, the priority is when instantiating. However, note that the prototype is not rewriting func, but is shared by the jsclass instance. Although its priority is not high, at the same time, we can also understand prototype and class definition members in this way:
<script>
function jsclass() {
= "never-online";
= function () {
alert('func');
}
}
= {
func : function () {
alert();
}
}
var a = new jsclass();
();
delete ;
();
</script>
Let's modify the above code. Look at it like this:
<script>
function jsclass() {
= "never-online";
= function () {
alert('func');
}
}
= {
func : function () {
alert(?:'no value');
}
}
var a = new jsclass();
();//Calling internal members
delete ;//Delete here the func defined inside the class is deleted here
();//Calling prototype member
delete ;//Try to delete func(prototype) again
();//Delete is invalid (the internal func has been deleted), and the output can still be printed
</script>
Note: Members within the class can be deleted with delete, while those defined in the prototype cannot be deleted with delete instance name or member name.
If defined with prototype, when instantiating: use prototype instance to specify the type of objects to be created, and create new objects by copying these prototypes
That is, on the above
delete ;//Delete here the func defined inside the class is deleted here
();//Calling prototype member
After that, call() again, and when called, it is implemented through the call. Instead of (), this also explains why there is no rewrite when defining func inside jsclass and when defining func in prototype.
1、
prototype is associated with Clone.
That is, when an instance is created, prototype clones members to the instance of the Class(function).
Detail: The most common prototypes in the built-in objects, such as: the Array prototype has the join and split method.
When creating array a var a=[1,2], all methods in the prototype are clone onto a.
2. This is an instance pointer of this class (this pointer is "dynamic bind"). How to understand the dynamic linkage of js this, please refer to the article I wrote: /?id=117
When creating an instance of this class, the instance has all predefined with similar members. It also has members defined in the prototype prototype. If the internal definition of the class is the same as one of the definitions in the prototype, it is not a rewrite:
Looking at this example, jsclass defines func defined in prototype. If there are members inside jsclass and the ones in the prototype, the priority is when instantiating. However, note that the prototype is not rewriting func, but is shared by the jsclass instance. Although its priority is not high, at the same time, we can also understand prototype and class definition members in this way:
<script>
function jsclass() {
= "never-online";
= function () {
alert('func');
}
}
= {
func : function () {
alert();
}
}
var a = new jsclass();
();
delete ;
();
</script>
Let's modify the above code. Look at it like this:
<script>
function jsclass() {
= "never-online";
= function () {
alert('func');
}
}
= {
func : function () {
alert(?:'no value');
}
}
var a = new jsclass();
();//Calling internal members
delete ;//Delete here the func defined inside the class is deleted here
();//Calling prototype member
delete ;//Try to delete func(prototype) again
();//Delete is invalid (the internal func has been deleted), and the output can still be printed
</script>
Note: Members within the class can be deleted with delete, while those defined in the prototype cannot be deleted with delete instance name or member name.
If defined with prototype, when instantiating: use prototype instance to specify the type of objects to be created, and create new objects by copying these prototypes
That is, on the above
delete ;//Delete here the func defined inside the class is deleted here
();//Calling prototype member
After that, call() again, and when called, it is implemented through the call. Instead of (), this also explains why there is no rewrite when defining func inside jsclass and when defining func in prototype.