When using object-oriented programming, the inheritance between objects is naturally unavoidable! And the prototype is a very important way to achieve JavaScript inheritance!
Let's start by looking at the following code:
Copy Code code as follows:
function person (name, age) {
THIS.name = name;
This.age = age;
}
Person.prototype.getInfo = function () {
Alert ("My name is" +this.name+ ", and I have" +this.age+ "years old");
JS programming is most commonly used in the definition of two object classes. Either of the following two methods can achieve the same effect! The following two methods are commonly used in daily JS programming:
1. Mixed constructor and prototype (emphasis)
The Code is as follows:
Function car (sColor, iNumbers) {// The constructor is only used to define all non-function attributes of an object, that is, attributes of an object.This. color = sColor
Summary of methods for obtaining prototype objects in Javascript, and javascript prototype
In Javascript, if we have an object but do not know its constructor, how can we obtain its prototype object?
In Chrome or FireFox, we can directly use the _ proto _ attribute of an object to obtain its prototype object.
Copy c
First, what is prototypeWhen a constructor is created, if a variable declaration is made within a function, the declared private variable is not accessible externally. This is used to declare instance variables and functions to ensure that objects declared by using the constructor can call these variables and functions. However, the life instance function replicates an identical function to the stack space each time a new object is created, which is a waste of resources.
Constructor Prototype pattern prototype mode (PHP example), Prototypepattern
When a class is mostly the same and only partially different, if you need a large number of objects of this class, it is expensive to instantiate those same parts each time, but if you create the same parts of the object before cloning, you can save overhead.
One way to implement PHP is to __construct () and initialize functions t
7. Builder, prototype, and Singleton of the Creation Mode ----- big talk design mode and the prototype of the two figures of the westward journey
I. Builder ModeSeparates the construction of a complex object from its representation, so that different representations can be created during the same construction process.The benefit of the builder mode is to separate the Construction Code from the representatio
, but the problem comes again, in the global scope to define a real just want to let the blog use the function, display to let the global scope some veritable, What's even more unacceptable is that many methods are defined in the global scope that are only used by specific objects, wasting space, and obviously losing object-oriented encapsulation, so you can solve this problem by prototyping.prototype ModeEach function we create has a prototype (
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 prototype object of object A is B, a
object itself as the first parameter. The difference between the two is the second argument: Apply passes in an array of parameters, which is the combination of multiple parameters into an array, and call is passed in as a call parameter (starting with the second argument).Closure Depth: http://www.cnblogs.com/onepixel/p/5062456.html1 prototype design patternClone () can be used in. NET to implement the prototype
Elementary analysis of PHP prototype model and analysis of PHP prototype
Prototype mode:
Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototype.Scenario: Classes have a lot of resources, performance and security requirements, and are used in combination with
Js drag prototype declaration and usage summary, js drag prototype Declaration
The following is a self-written prototype declaration about js Drag and Drop: the code is as follows:
Notes:
1. Point this to who it actually points to-find out the object it refers
2. Use of the call () method
3. What is the difference between assigning a parent
problem again, in the global scope of the definition of a real only want to use the blog function, show that the global scope of some worthy, What is even more unacceptable is that many methods are defined in the global scope that are used only for specific objects, wasting space, and obviously losing object-oriented encapsulation, so you can solve this problem by prototyping.
Prototype mode
Each function we create has a
Analyze the php prototype mode and the php prototype. Analyze the php prototype mode and the php prototype mode: Use the prototype instance to specify the object type, and copy the prototype to create a new object. Application sce
Good description from "dead of winter"
The prototype-oriented language idea is exactly the same as the prototype mode: The objects cloned from the same prototype are a class of objects. The prototype-oriented language provides language-level support for this mode, that is, all the definitions of "classes" are implemen
I. IntentionSpecify the type of object to create with a prototype instance, and create a new object by copying the prototypes Second, the prototype pattern structure diagram
Third, the main role in the prototype modelAbstract prototype (Prototype) role: Declares an interf
Prototype and inheritance: prototype inheritance
Prototype and inheritance
1. prototype
A) prototype is the sub-object of all objects.
B) It can only be used after an object is created.
C) if you add attributes and methods to protot
Encountered in the interview, ask JS how to achieve inheritance, in fact, the best way is the constructor + prototype, today in the discussion, found that their previous understanding of some of the misunderstandings, specifically written out, recently are relatively busy, etc. hand on the project done, you can do a summary.First of all, I didn't know the place before.1 __proto__ This is not a standard implementation, but the major browsers and node h
How do new product managers choose a prototype tool and product manager prototype?
One of the most important skills of a product manager is to skillfully use prototype tools to draw excellent product prototypes. However, new product managers often have a misunderstanding: what others use and what I learn. For example, if you see someone else using Axure, you will
If you redefine the classes in the previous example in a prototype way, they will change to the following form:
Copy Code code as follows:
function ClassA () {
}
ClassA.prototype.color = "Blue";ClassA.prototype.sayColor = function () {alert (This.color);};
function ClassB () {}
Classb.prototype = new ClassA ();
The magic of the prototype approach is the last line of code. He
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.