Discover javascript class vs prototype, include the articles, news, trends, analysis and practical advice about javascript class vs prototype on alibabacloud.com
There are many types and implementations of inheritance in JavaScript in many books, basically two kinds: class inheritance (object impersonation) and prototype inheritance.Class inheritance (Object impersonation): The method of defining its own properties inside a function, when the subclass inherits, using call or apply to implement the object impersonation, th
/* Class attributes and Object Attributes */Array. Info1 = "Info1"; // adds the class attribute Info1 to the Array.Array. prototype. Info2 = "Info2"; // adds the object attribute Info2 to the Array.
Arr = [1, 2, 3];Alert (arr. Info1); // undefinedAlert (arr. Info2); // Info2
Alert (Array. Info1); // Info1
/* Class m
Original article: How JavaScript Prototype Really Works
This is the second article in The JavaScript technology series. I plan to discuss in depth the most confusing JavaScript prototype object and how to use it to implement inheritance.In the previous article, we analyzed
What is the prototype chain??? The concept of C/c++/java class has been deep in my heart, and suddenly this is not suitable. What's the use?And see the back ES6 grammar, unexpectedly also joined the class?????What the hell? Isn't that a hit on your face?Maybe there's something useful in the back, but I haven't found it yet.A brief explanation of the
The prototype of the javascript class. Here I would like to put it simply: javascript itself does not have strict Inheritance like java. We say that javascript inheritance, it is also similar to the object-oriented language, so although
http://www.link888.cn/index.php?load=readid=500
For JavaScript, the method is essentially a property.
The construction method , like the construction property, is stored separately in each instance and is completely independent of each other.
The prototype method , like the prototype property, is stored in the prototype
We have used prototype property simulation class and inheritance implementation in Chapter 1. The prototype attribute is essentially a JavaScript Object. Each function has a default prototype attribute.
If this function is used to create a custom object, we call it a constru
do is often incredible, and some functions can even be equivalent to the desktop program.
Now we can start with a javascript development framework prototype_1.3.1 (prototype. I would like to introduce the advanced javascript applications first, but I am afraid that the level is not enough and that it is not organized. Therefore, combined with
This article describes the prototype attribute in JavaScript. This attribute is an important basis for understanding JavaScript classes and inheritance. We have used prototype property simulation class and inheritance implementation in Chapter 1. The
do is often incredible, and some functions can even be equivalent to the desktop program.
Now we can start with a javascript development framework prototype_1.3.1 (prototype. I would like to introduce the advanced javascript applications first, but I am afraid that the level is not enough and that it is not organized. Therefore, combined with
Prototype mode each function (not exactly a class, an object.) has a prototype property, which is a pointer to an object. Using the prototype object'sBenefitsIs that you can have all object instances share the properties and methods that it contains. 1. Prototype objects (1)
. Because this hybrid uses the prototype chain, the instanceof operator can still run correctly.Yang ZhiwangLinks: https://www.zhihu.com/question/20289071/answer/14644278Source: KnowCopyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.LZ to understand the existence of call and apply the reason, to remember a bit:In JavaScr
Prototype chain is the most important way to inherit,Principle: Each class (constructor, JS function) has a prototype attribute (prototype) point to a prototype object, the prototype object has a constructor (constructor), it refe
If you have been in touch with JS for a long time, then you should be able to see whether I summed up the correct, if you have just started to learn, then through my summary, you can distinguish them faster, remember I just contact JS, this piece is fuzzy for a long time!
1, the difference of looks:
1 function Class () {//Declaration of a class
2 This.constructmethod = function () {}; Adding a constructe
In object-oriented programming JS script, there are two main ways to define instance methods
As follows:
function ListCommon2 (Afirst)
{
var first=afirst;
This.do1=function ()
{
alert (' +first ');
}
Listcommon2.prototype.do2=function ()
{
// alert ("+first")//error, no access to the A-
this.do1 () ;
}
What's the difference between this.do1=function () and listcommon2.prototype.do2=function ()?
is equivalent to a
has been using JS to write self-righteous object-oriented approach, encountered a problem, is to define a method, as follows:
function ListCommon2 (First,second,third)
{this
. First=function ()
{
alert (' +first ');
}
Listcommon2.do1=function (a)
{
//this . A ();
Alert ("the" +first);
}
Listcommon2.prototype.do2=function (a)
{
//this. A ();
Alert ("the" +first);
}
What is the difference between the two methods? What's the use of not
After reading the basic Ajax tutorial, take notes.
I have not understood prototype inheritance and class inheritance. The following describes the generic functions written by Bob Clary:
Function createinheritance (parent, child) {var property; For (property in parent) {If (! Child [property]) {Child [property] = parent [property] ;}}
Bob Clary'sCodeFrom Page 148th of Ajax basic tutorial. This function is
1. Using Javascript eval Method.2. using a temporary Ables save object "this" reference.3. in innerHTML, we can using String to pass the prototype Method.E.g.
1, Using Javascript eval method.
2, using a Veriables Save Object "This" reference.
3, in InnerHTML, we can using a String to pass the prototype method.
e.g.
JavaScript exploration: Powerful prototype and prototype chain, and javascript exploration prototype
// The foo variable is for (var I in foo) {if (foo. hasOwnProperty (I) {console. log (I) ;}} in the preceding example );}}
JavaScript
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.