Prototype1.5.1 source code interpretation and analysis-4

Source: Internet
Author: User
Class source code

The second code of the prototype framework source code is as follows:

VaR class = {

Create: function (){

Return function (){

This. Initialize. Apply (this, arguments );

}

}

}

Syntax

What is a function? A function is an executable JavaScript code segment defined by a javascript program or predefined by JavaScript. Although a function is defined only once, javascript programs can execute or call it multiple times. A JavaScript function carries actual or formal parameters. It is used to specify one or more values to be used for execution and computation. It can also return a value to indicate the computation result. The javascript language provides many predefined functions, such as math. Sin (), which are used to calculate the sine of the angle.

The actual parameter of the function: arguments object. In a function, the identifier arguments has a special meaning. It is a special attribute of the called object and is used to reference the arguments object. The arguments object is like an array. You can obtain the parameter values passed to the function according to numbers. But it is not a real array object. The arguments object also defines the callee attribute. Although there are fixed numbers of named parameters when defining JavaScript Functions, the number of parameters passed to this function can be arbitrary. The array arguments [] allows full access to the actual parameter values, even if some parameters are not named. Assume that you have defined a function f and want to pass it to the actual parameter X. If you use two actual parameters to call this function, then in the function body, you can use the format parameter name X or arguments [0] to access the first actual parameter. The second actual parameter can only be accessed through arguments [1. As with all arrays, arguments has the Length attribute, indicating the number of elements contained in it. Therefore, in the body of function f, if two actual parameters are used during the call, arguments. the value of length is 2. here, we have used the "arguments array" multiple times, which is inaccurate. Remember that arguments is not a real array. It is an arguments object.

The arguments object has an extraordinary feature. When a function has a named parameter, the array element of the arguments object is a synonym for the local variable that stores the function parameter. Arguments [] arrays and named parameters are two different methods that reference the same variable. Changing the value of a parameter with the parameter name also changes the value obtained through the arguments [] array. Changing the parameter value through the arguments [] array also changes the parameter value obtained using the parameter name.

Ecmascript V3 defines two call () and apply () methods for all functions (). You can use these two methods to call a function just like calling methods of other objects. Call () and apply () are the objects of the function to be called. In the function body, this parameter is the value of the keyword "this. The remaining parameters of call () are the values passed to the function to be called. For example, to pass two numbers to the function f () and call it as the method of object o, you can use the following code:

F. Call (O, 1, 2 );

The apply () method is similar to the call () method, except that the parameters to be passed to the function are specified by the array.

Javascript has the concepts of objects and classes, but it does not have the built-in inheritance concept. We can use constructors to implement functions similar to built-in inheritance. In the constructor, anything that is set to the attribute of this can be used as a member of the object. The constructor has two features: it has a new operator call; it is passed to it as a reference to the newly created null object, and the reference is used as the value of the keyword "this, it also needs to initialize the newly created object.

Code parsing and usage

Class is a global object. Its only method is create, which returns a function similar to Ruby class. Prototype distinguishes functions from classes by using a global object class. Class. create () Only returns an empty class. By default, this class has the initialize method. To use this class, at least one constructor is required, this requires class inheritance. (The author's note ---- this explanation may be a bit difficult to understand. I am at a low level and only understand this level .)

Example:

VaR animal = Class. Create ();

Animal. Prototype = {

Initialize: function (name, sound ){

This. Name = Name;

This. Sound = sound;

},

Speak: function (){

Alert (name + "says:" + sound + "! ");

}

};

VaR snake = new animal ("ringneck", "hissssssssss ");

Snake. Speak ();

//-> Alerts "ringneck says: hissssssssss! "

VaR dog = Class. Create ();

Dog. Prototype = object. Extend (new animal (),{

Initialize: function (name ){

This. Name = Name;

This. Sound = "woof ";

}

});

VaR Fido = new dog ("Fido ");

Fido. Speak ();

//-> Alerts "Fido says: Woof! "

# JavaScript/ajax Columns

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.