Home >Web Front-end >JS Tutorial >Regarding the prototype issue of Javascript. _javascript skills

Regarding the prototype issue of Javascript. _javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 19:21:541013browse

prototype

1.
prototype is associated with Clone,
that is, when an instance is created, prototype will clone the members to the instance of the Class (function).
Detail: The most common prototypes in several built-in objects, such as: Array prototype has join, split methods,
When creating array a var a=[1,2], all methods in the prototype are Clone to a.

2. This is the instance pointer of the class (this pointer is "dynamically bound"). How to understand the dynamic binding of js this, please refer to this article I wrote: http://blog.never-online.net/article.asp?id=117
When creating an instance of this class, the instance has pre- All members defined similar to this.p. It also has members defined in the prototype prototype. If the internal definition of the class is the same as a definition in the prototype, it is not overridden:

Look at this example, this.func defined by jsclass, and func defined in the prototype , if there are members inside jsclass that are the same as those in the prototype, the priority during instantiation is this.func, but note that func is not rewritten in the prototype, but is common to jsclass instances, although its priority is not as high as this.func. At the same time, we can also understand prototype and class internally defined members in this way:




Let’s modify the above code again. Look at it this way:



Note: Members inside the class can be deleted using delete, but those defined in the prototype cannot be deleted using delete instance name. member name.
If defined with prototype, when instantiating: use the prototype instance to specify the type of object to be created, and create new objects by copying these prototypes
That is, the above
delete a.func;//this Delete the func defined inside the class <script> <BR>function jsclass() { <BR> this.p = "never-online"; <BR> this.func = function () { <BR> alert('func'); <BR> } <BR>} <BR>jsclass.prototype = { <BR> func : function () { <BR> alert(this.p); <BR> } <BR>} <BR>var a = new jsclass(); <BR>a.func(); <BR>delete a.func; <BR>a.func(); <BR></script>a.func();//After calling the prototype member <script> <BR>function jsclass() { <BR> this.p = "never-online"; <BR> this.func = function () { <BR> alert('func'); <BR> } <BR>} <BR>jsclass.prototype = { <BR> func : function () { <BR> alert(this.p?this.p:'no value'); <BR> } <BR>} <BR>var a = new jsclass(); <BR>a.func();//调用内部成员 <BR>delete a.func;//此处删除是的类内部定义的func <BR>a.func();//调用prototype成员 <BR>delete a.func;//试图再次删除func(prototype) <BR>a.func();//删除无效(内部的func已经被删除),依然可打印输出 <BR></script>, call a.func() again. When calling, it is implemented by calling prototype.func. It is not a.func(), which also explains why there is no rewriting when defining func inside jsclass and defining func in prototype.​

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn