Javascript Object.extend_js Object-oriented

Source: Internet
Author: User
Since it is a class, there is an abstract class, a concrete class, a class inheritance, while a class member can have instance members and static members. Here's a look at how prototype do this.
First look at the following code in the prototype:
Copy Code code 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,object is actually a function, he does not have any members, so is an empty class, so abstract also has no members. This is not said for the moment, and you can see that this is the basis of the abstract class. Explain the following syntax first:
Function.member=function () {}
In this case, the function is generally defined, and the function of this statement is to add a static member to the function member,member the content is followed by the equals sign. As the second paragraph of code above object.extend= ..., is to add a static method to the object class extend. OK, we know how to define a static member of a class, then you must be very interested to know how the instance member is defined, quite simply, by adding prototype between the class name and the member name:
Function.prototype.member=function () {}
Prototype can be used not only in this way, but also:
Copy Code code as follows:

function.prototype={
Member1:function () {...},
MEMBER2: "ABC",
Member3:function () {...}
}

This is the implementation of the definition of instance members. But what does prototype mean? As I said in the first article, it is directly enclosed in {}, representing an object, such as Prototype,class, which is defined as a global object. And look at the following usage, prototype is a {} structure, is it also an object? Yes, yes, prototype is actually an Object! In JavaScript, an object can be arbitrarily added to its members, using the following syntax:
Object.member=function () {...};
As long as this definition is used, an object can immediately have the member's Method! JavaScript is so magical!
Well, now that we know that prototype is an object, and a function is a functional or a class, then we can assume that prototype is a static member that is retained internally by any class (function). Its function is to store all members of this class pointer, but these members are only prototypes, not initialized, which also conforms to the prototype of the original meaning. You can expand the membership at any time by prototype this object. When you new a class, the members of the prototype are initialized and then assigned to the instantiated object.
The third paragraph of code above object.prototype.extend= ..., is to add an instance method to object extend, the instance method can refer to the this pointer, pointing to the object itself instantiated by this class. Of course, this object has a member extend.
Before you go on, take a look at two statements:
For (var p in object) {}
Method.apply (object,arguments);
The first sentence: Enumerate all members of a variable, if it is a function, then all static members; If it is an object, that is all instance members, and the type of P is a string. Represents the name of a member. Not only can you use Variabel.member, you can also use variabel["member". Conversely, the assignment is the same. This provides a great convenience for enumerating members of a variable.
The second statement: The method is applied to object to execute, and the argument is arguments this array. Note: method is not a member of object. However, we can assume that the execution of this statement means: Object.Method (arguments). This is a very important method, which will be used frequently, and you will become familiar with it.
The following continues extend, which is a very important way to see that it is both a static member of class object and its instance member, so what does it do? Let's see: It Takes two parameters, destination and source, and if destination and source are classes, its function is to copy all the static members of the class source to the class destination, If both destination and source are objects, copy all instance members. If there is already a member of the same name in destination, the member will be overwritten. That is, let destination have all the members of source, and the function returns the destination. Here's a look at extend as an instance member of object:
Object.prototype.extend = function (object) {
Return Object.extend.apply (this, [this, Object]);
}
Beginning a little dizzy, but do not hurry, still can understand, apply grammar has just been said, its caller is a method, And Object.extend is a static method that is applied to this, which is an instance of object, assumed to be obj, followed by an array of square brackets, including 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 the expression itself. What is Object? parameter, well, is the instance method extend from the parameters, do not confuse. What about extend? OBJ does not define a extend instance member, but by using apply, it can use the static member extend of object, and then 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, object is also objects, that is, destination and source are objects, so the function is to make obj have all members of object. and will return obj. It sounds a bit clumsy, but the logic is simple: let obj "inherit" from object! Well, we saw the inheritance, but you will certainly ask, the object of inheritance, the first heard Ah, we speak of inheritance are all about the inheritance of class. Yes, it is true that there is no real class inheritance yet, but it is already in sight: The class doesn't have a prototype, and prototype is the Object!
Well, with this in mind, the inheritance syntax for classes seems simple:
B.prototype.extend (A.prototype);
Let b inherit a.
But the fact is not so simple: prototype is the storage method prototype pointer, extend method is not initialized, can not be used! To use extend, you must instantiate an object. Let's see how the prototype do it:
B.prototype= (New A ()). Extend (B.prototype);
That's a brilliant idea! Fully explain the function is actually a variable of the truth. Instantiate a object first, then invoke extend on it, cover all members of the member B.prototype to the object of a, and then assign the object A to B.prototype. Completed the work of B inheriting from a. In practical use, the general usage is:
B.prototype= (New A ()). Extend ({});
Since a b inherits from a, usually B is an undefined class, the class member can actually be defined in the later {}. Of course, you can also first define, then inherit, but with the traditional concept of a difference.
OK, today to write here very tired, it is estimated that the person is also, hehe. Now we have a basic understanding of the prototype class development framework, you can see some advanced applications, the next round goodbye:
Copy Code code as follows:

<script language= "JavaScript" >
//Adding static methods to object objects extend, The method's as copy source has all properties and methods to the destination
Object.extend = function (destination, source) {
for (property in source) {
Destination[property] = Source[property];
}
return destination;
}

var dog = function (name)
{
THIS.name = name;
}
///Copy Printname method to Dog.prototype
Object.extend (Dog.prototype,
{
Printname:function ()
{
Alert (this.name);
}
}
);
var a = new Dog ("Dog");
A.printname ();
</script>

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.