JavaScript Object. Extend

Source: Internet
Author: User

Since it is a class, there is an abstract class, a specific class, and class inheritance. At the same time, the class members can have instance members and static members. Let's take a look at how prototype achieves this.
First look at the following in prototype Code : Copy code The Code is as follows: var abstract = new object ();
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
Object. Prototype. Extend = function (object ){
Return object. Extend. Apply (this, [This, object]);
}

The first declares an object abstract. An object is actually a function and has no members. Therefore, it is an empty class, so abstract has no members. This is the basis of the abstract class. First, explain the following syntax:
Function. Member = function (){}
In this case, the function is generally defined. This statement adds a static member Member to the function, and the content of the member is after the equal sign. For example, the second code above is object. Extend = ......, It adds a static method extend to the object class. OK. Now that we know how to define static members for a class, you must be very curious about how to define the instance members. It is very easy to add prototype between the class name and the member name:
Function. Prototype. Member = function (){}
Prototype can not only be used in this way, but also:Copy codeThe Code is as follows: function. Prototype = {
Member1: function (){......},
Member2: "ABC ",
Member3: function (){......}
}

This defines the instance members. But what does prototype mean? As I said in the first article, it is directly included in {} to represent an object, such as prototype and class, which are global objects defined in this way. In the following usage, prototype is followed by a {} structure. Is prototype also an object? Yes, that's right. prototype is actually an object! In JavaScript, we can add any member of an object, using the following syntax:
Object. Member = function (){......};
After such definition, an object can immediately use the member method! Javascript is so amazing!
Well, now we know that prototype is an object, and function is a function or class. We can think that prototype is a static member that is retained internally by any class (function. Its function is to store all member pointers of this class, but these members are only prototype and are not initialized. This is also in line with prototype. You can use the prototype object to expand members at any time. When a new class is created, prototype members are initialized and assigned to the instantiated object.
The third code above is object. Prototype. Extend = ......, This is to add an instance method extend to the object. The instance method can reference this pointer and point to the object instantiated by this class. Of course, this object has the extend Member.
Before proceeding, let's take a look at two statements:
For (var p in object ){}
Method. Apply (object, arguments );
The first sentence is to list all the members of a variable. If it is a function, it is all static members. If it is an object, it is all instance members. The P type is a string. The name of the member. You can reference a member not only using variabel. Member, but also using variabel ["member"]. In turn, assignment is also true. This makes it easy to enumerate members of a variable.
The second statement: apply the method to the object for execution. The parameter is the arguments array. Note: The method is not a member of the object. However, we can think that the execution of this statement means: object. Method (arguments ). This is a very important method, which will be frequently used later and you will gradually become familiar with it.
Next, let's continue with Extend. It is a very important method. We can see that it is both a static member of the class object and a member of its instance. What does it do? Let's take a look: it receives two parameters: destination and source. If destination and source are both classes, its function is to copy all static members of class source to class destination, if destination and source are both objects, all instance members are copied. If a member with the same name already exists in destination, the member will be overwritten. That is to say, Let destination have all the members of the source, and the function returns this destination. The following describes how to use extend as an instance Member of an object:
Object. Prototype. Extend = function (object ){
Return object. Extend. Apply (this, [This, object]);
}
It was a bit dizzy at first, But don't worry, you can still understand it. The apply syntax has just been mentioned, and its caller is a method, while the object. extend is a static method. It is applied to this, that is, the object instance. If it is OBJ, square brackets are an array that contains two members, this and object. This array is actually the arguments parameter of the object static member extend. Then this statement is equivalent to executing
OBJ. Extend (this, object );
This does not explain. It indicates itself. What is an object? Parameter. Well, it is a parameter sent from the instance method extend. Do not confuse it. What about extend? OBJ does not define extend instance members, but with apply, it can use the static extend of the object. Let's look at the extend function body:
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}
Because obj is an object and object is an object, that is, destination and source are both objects, the function is to make OBJ have all the members of the object. And obj is returned. It sounds a bit difficult, but the logic is simple: Let OBJ "inherit" object! Well, we have seen inheritance, but you will certainly ask, for the first time, we heard that inheritance is about class inheritance. Yes, I do not see the real class inheritance yet, but it is close at hand: Isn't there a prototype for a class, and prototype is an object!
Well, with this in mind, the inheritance syntax of the class seems very simple:
B. Prototype. Extend (A. Prototype );
Let B inherit.
The fact is not that simple: Prototype is used to store the method prototype pointer. The extend method is not initialized and cannot be used! To use extend, you must instantiate an object. Let's see how prototype works:
B. Prototype = (new A (). Extend (B. Prototype );
A brilliant way! It fully demonstrates that a function is actually a variable. Instantiate the Object first, then call extend based on it, overwrite all B. Prototype members to the object of A, and then assign this a object to B. prototype. Completed B's work inherited from. In actual use, the general usage is:
B. Prototype = (new A (). Extend ({});
Because B inherits from a, B is usually an undefined class before B, so class members can be defined in. Of course, you can also define and inherit first, but it is different from the traditional concept.
OK. I am tired of writing it here today. It is estimated that the person who looks at it is also, haha. Now we have basically understood the prototype class development framework. You can see some advanced applications. See you next round :) Copy code The Code is as follows: <script language = "JavaScript">
// Add the static method extend to the object. The method as a copy source has all attributes and methods to destination.
Object. Extend = function (destination, source ){
For (property in source ){
Destination [property] = source [property];
}
Return destination;
}

var dog = function (name)
{< br> This. name = Name;
}< br> // copy the printname Method to the dog. prototype
object. extend (dog. prototype,
{< br> printname: function ()
{< br> alert (this. name);
}< BR >}< br>);
var A = new dog ("dog");
. printname ();

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.