In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the prototype of Javascript object". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the prototype of Javascript object?"
JavaScript is often described as a prototype-based language (prototype-based language)-each object has a prototype object that inherits methods and properties from its prototype.
A prototype object may also have a prototype and inherit methods and properties from it, layer by layer, and so on. This relationship, often referred to as prototype chain, explains why an object has properties and methods defined in other objects.
Object prototype Prototype
1. Method overload
Create a kitten constructor with the following code:
Function Cat (name,color) {this.name = name;this.color = color; this.run=function () {alert ("a kitten" + this.color + "galloped over...");} this.eat=function () {alert (this.name + "want to eat fish");}} var cat1 = new Cat ()
All of the above use this to define methods, and this represents a new instance, and a copy of the method is created for the new instance when it is created.
Isn't it a little superfluous? how to solve it?
Analysis: it is a bit wasteful to define the features of each type at the instance level, so it would be nice if each instance could be defined at the class level and each instance automatically has the common characteristics of the class. We're going to use prototype here.
2. The use of prototypes
2.1, prototype properties
In JavaScript, the function itself is an object that contains "methods" and "properties". For example, I learned some methods (such as constructor ()) and properties (such as name and length) and so on.
Now let's introduce a new attribute-- prototype Prototype.
Each function we create has a prototype property that points to an object whose purpose is to contain properties and methods that can be shared by all instances of a particular type.
/ / define the number of formal parameters of a constructor function Person (name,age) {} / / function console.debug (Person.length) / / = > 2max / constructor console.debug (Person.constructor) / / = > Function () / / prototype type console.debug (typeof Person.prototype) / / = > object// prototype content console.debug (Person.prototype) / / ↓↓
Each class (constructor) has a prototype property, and all the properties of the prototype object that created the instance of the class are immediately assigned to the object to be created.
2.2, prototype operation
Set value:
Constructor. A prototype. Attribute = property value constructor. A prototype. Method = function
Value:
Object. Property object. Method ()
2.3. Priority of attribute access
Native attributes take precedence over prototype attributes. Follow the search from top to bottom:
2.4.The mysterious _ _ proto__ attribute
Access the properties above the object, directly through object.name.
The magic user.__proto__ property, which is actually the prototype property of the corresponding User class.
Console.debug (user.__proto__===User.prototyp); / / = > true
The _ proto_ property belongs to the object instance, the property of the prototype property class.
After each object is created, it automatically creates a reference to the prototype, giving the object all the characteristics of the type prototype.
A member of the _ _ proto__ (prototype) property of an object, which can be directly passed through object. Members to visit.
At this point, I believe you have a deeper understanding of what the Javascript object prototype is, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.