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 accessing the object's this value, and then the constructor can return the object it created to us. In this way, we have a way to give new obj
We know that a prototype is an object that other objects can use to implement property inheritance, in addition to prototype, and __proto__1. The difference between prototype and __proto__Prototype is a function -only attribute __proto__ is a property of each objectBut __proto__ is not a canonical attribute, only part of the browserThis property is implemented an
Doubts come from:
Copy Code code as follows:
function Foo {
this.name = ' Foo ';
}
Alert (Foo.prototype = = Function.prototype); False I was wondering why Foo's prototype was not function.prototype.
The following example lets me take it for granted that O.prototype = = Function.prototype should be true:
Copy Code code as follows:
function foo () {
this.name = ' Foo ';
}
Function.prototype.sayHello =
1 What is prototype
The prototype property of an object in JavaScript that returns a reference to the object type prototype. This is a rather awkward explanation, and to understand it, the concept of object type (type) and prototype (prototype) should be understood first.
B
JavaScript does not support inherited patterns of traditional classes, it is a prototype-based inheritance, that is, through prototype settings to implement inheritanceLet's consider the following question function person () { this . Name = "haha" ; this . Sayname = function () {Console.log ( this .name); }} var p1 = new
person ();
var p2 =
new
person (); P1.say
The prototype mode belongs to the creation mode of the object. By giving a prototype object to indicate the type of all objects created, and then creating more objects of the same type with the method of copying the prototype object.The prototype model has two forms: simple form and boarding form.Primitive model patter
JavaScript is a dynamic language (Dynamic Language Dynamics Programming Language: Dynamic type language, meaning that the type of check is done at run time, that is, "weak type" language), there is no class concept, there are classes reserved words, But cannot be used as a variable namePrototype: Each object in JavaScript has an internal private connection pointing to another object, which is the prototype of the original object note: The
Every object in JavaScript has a prototype property.Object methods, class methods, prototype methods1, Object method: Understanding is very simple, mainly if the class generates an instance, then the instance can use this method2. Class method: A method that does not need to be used to generate an instance3, the prototype method: mainly used to expand the JS exis
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD">HTML> Head> Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /> title>Untitled Documenttitle> Scripttype=text/javascriptCharSet=utf-8> //Customizing the prototype object alters the constructor of the prototype object. //Concept of Prototypes: All properties and meth
First, describeThe prototype pattern is a prototype object that identifies the type of object to be created, and then copies the creation of more homogeneous objects using the method of copying the prototype object. For example, we have an object in the dynamic process of the program, which contains a series of valid data, we need a new object that is exactly the
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 JavaScript, let's look at the familiar common object-oriented language. In 23 design patterns, you will remember that there is
Axure Pro is a tool designed to create an interface prototype.
When we communicate with users about the requirements of the software system, if we use text, the general end user is difficult to understand and imagine your future product appearance. Therefore, we usually design the interface prototype, the interface prototype expresses what the software system can
Prototype mode is the creation mode.
Design intent: Use the prototype instance to specify the type of object to create and create a new object by copying the prototype.
We use the class diagram of the example that builds the CV to illustrate the prototype pattern.
Class Diagram:
The
To better understand this article, please learn the basic knowledge of prototype and prototype chain in advance.
Let's take a look at the following questions about the pitfalls:
1 Object.__proto__ == Object.prototype2 Function.__proto__ == Function.prototype3 Object instanceof Function4 Function instanceof Object
A lot of people may be confused at first glance (including the author). It feels a bit like an
Read the study notes in Chapter 2nd of JavaScript advanced programming (version 6th. The prototype has a problem in the reference type:
Function supertype () {} supertype. prototype. color = ['red', 'Blue ', 'green']; var instance1 = new supertype (); instance1.color. push ('black'); var instance2 = new supertype (); console. log (instance2.color );
The prototype
// When there are many attributes and methods, writing is not very convenient. You can write them in json format.// Because the prototype is not specified through Person. prototype, the constructor will not point to Person but to Object.// If constructor is really important, you can describe the prototype in json.Function Person (){}
Person.
Although the prototype pattern is a pattern of creation, it has nothing to do with the engineering model, and the idea of the pattern is to copy and clone an object as a prototype, and create a new object similar to the original object. This summary will be explained by the copy of the object. In Java, the Copy object is implemented through clone (), and a prototype
Speaking of prototype, we have to say the new process first.Let's look at this piece of code first:
1
A very simple piece of code, let's see what this new has done? We can split the new process into the following three steps:The key is the second step, and let's prove it:
1
This piece of code returns TRUE. Explain the correctness of step 2.So what is __proto__? Let's simply say here. Ea
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 can see the code in detail (here's a simple package):function Runtochain (obj) { var ch
Constructors Create objectsLet's first use the constructor to create an object:function Person () {} var New = ' name '// nameIn this example, person is a constructor, and we create an instance object person with new.It's easy, then, to get to the point:PrototypeEach function has a prototype attribute, which is the prototype that we often see in various examples, such as:function Person () {} // Although
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.