A simple introduction to JavaScript's prototype prototype:Prototype prototype is a particularly important concept of JavaScript, belongs to have to master, if there is no good grasp, further use or learn JS Basic is impossible to achieve things, and this concept is a little difficult, may be the first contact with a friend is a bit difficult, The following is a b
prototype property, property value is a normal object
All functions have a prototype/display prototype property, and the property value is also a normal object The __proto__ property value for all reference types The property value for
Java has run-time type recognition, JS can be very convenient to imitate this feature, because all JS objects have a property constructor (constructor), representing the method of constructing the object, the prototype and the constructor method the same name, so you can know the prototype name of any object here. You
Special attention should be paid to reading and modifying the prototype in javascript, because the reading and writing of the prototype is not equivalent.
For members inherited from prototype objects, the read and write operations are inherently unequal. For example, assume that the
1. in prototype mode 1.1, each function we create has a prototype attribute, which is a pointer pointing to an object, the purpose of this object is to include attributes and methods that can be shared by all instances of a specific type. A simple explanation of the above is: first, we... syntaxHighlighter. all (); 1.
Person.prototype. The p1.constructor also points to object. But P1 is obviously generated by the constructor programmer, which causes the confusion of inheritance, so we have to manually fix the constructor, which is the code below.Programmer.prototype.constructor = Programmer;OK, now let's take a look at the prototype chain:Console.dir (p1);The result of this code isIt can be seen that P1 is an example of
'}, Getweight:function(){ return' Weight is 1kg ' }}//equivalent to Foo.property = new Object (), and then define the method ...varFoo5 =NewFoo (); Foo5.sayname ()//error is not found in this method.//But the original example could still be connected to the original prototype.Foo4.sayname ()//' A way to cover a new prototype 'foo.prototype.__proto__= = Object.prototype//true//In addition, the pro
To understand the prototype in JS, we must first clarify the following concepts1. All the things in JS are objects
2. All things in JS are derived from object, that is, the end point of all the prototype chain points to Object.prototype
["Constructor", "ToString", "tolocalestring", "valueof", "hasOwnProperty", "isprototypeof",
//" propertyIsEnumera
Before I was in the prototype chain of JavaScript, prototype inheritance and identifier lookup were somewhat confusing,
For example, the following code:
Copy Code code as follows:
function Foo () {};
var foo = new Foo ();
Foo.prototype.label = "Laruence";
alert (Foo.label); Output:laruence
alert (Foo.label);//output:undefined
To
View plaincopy to clipboardprint?/*Each object instance has a prototype property member used to point to its instanceof object (temporarily called parent object)We call this layer-by-layer relationship pointing to the parent prototype [p
JS in each of the creation of a function, the function will automatically have a prototype property, why that??Because the object on the root has a prototype property, and all the objects in JS inherit from object, so all the objects in JS have a prototype attribute, and in
The prototype of Javascript is always confusing. No matter how experienced experts or the author himself, he often shows limited understanding of this concept, I think this confusion has emerged when we first came into contact with prototypes. They are often related to new and constructor, especially the prototype property of functions ). In fact,
In JavaScript, the function itself is also an object that contains methods and properties.Length returns the number of arguments.In the 4th chapter, we have learned how to define a constructor function and use it to create a new (constructed) object. The main intent of this approach is to invoke the function with the new operator to achieve the purpose of accessi
In JavaScript, this is defined when created. What does this sentence mean?
function Foo () {
this.name = ' Peter ';
}
Foo ();
Console.log (name);//this is the global object when it is created, so the name here is Peter;
So what's different about adding a keyword new?
Let's say varobj = new Foo ();
In JavaScript, the new keyword does three things:
First, genera
Motive (motivation)In software systems, it is often faced with the creation of "some complex objects".These objects often face drastic changes due to changes in demand, but they have a fairly consistent interface. How to deal with this change? How to isolate "these volatile objects" from "Client programs (using these objects)" so that "client programs that rely on these volatile objects" do not change as demand changes.Intentions (Intent)Use the prototype
Basic explanation of JavaScript inheritance (prototype chain, borrow constructor, hybrid mode, original type inheritance, parasitic inheritance, and parasitic combined inheritance) and javascript Constructor
It's a long time to explain JavaScript inheritance. If you don't talk nonsense, go to the topic.
Now that you wa
In a typical object-oriented language, such as Java, there are classes (class) concepts, classes are templates of objects, objects are instances of classes. But in the JavaScript language system, there is no concept of class, JavaScript is not based on ' class ', but is implemented through constructors (constructor) and proto
1. Overview of the disadvantages of 1.1 constructors JavaScript generates new objects through constructors, so constructors can be treated as templates for objects. The properties and methods of the instance object can be defined inside the constructor function Cat (name, color) {this.name = Name;this.color = Color;this.meow = function () {Console.log(' Mew, Mew, Mew ... ');};}var cat1 = new Cat (' Da Mao '
In "JavaScript Play Inheritance (ii)", I used the prototype inheritance method to implement JavaScript inheritance, and the prototype is what the mystery. In this article, I will focus on the prototype to start the discussion.
Aside from
Explanation of the original JavaScript model and prototype chain
This article mainly introduces the original JavaScript model and prototype chain. This article describes the basic concepts of private variables and functions, static variables and functions, instance variables and functions,
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.