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 code in prototype:
Copy codeThe 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 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 codeThe 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)
{
This. name = name;
}
// Copy the printName method to dog. prototype
Object. extend (dog. prototype,
{
PrintName: function ()
{
Alert (this. name );
}
}
);
Var a = new dog ("dog ");
A. printName ();
</Script>

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.