Prototype Class. create FUNCTION parser _ prototype-js tutorial

Source: Internet
Author: User
The Class in Prototype is generally created using the Class. create method, for example, PeriodicalExecuter type. You can call newPeriodicalExecuter (xxx) to generate an object. The Code is as follows:


/**
* A well-designed timing actuator
* First, a PeriodicalExecuter type is created by Class. create,
* Configure the prototype in the syntax of the object quantity directly.
*
* It must be particularly noted that the rgisterCallback method calls the previously defined function prototype method bind and passes itself as a parameter.
* This is because setTimeout always uses the window object as the current object by default, that is, if the registerCallback method is defined as follows:
* RegisterCallback: function (){
* SetTimeout (this. onTimerEvent, this. frequency * 1000 );
*}
* Therefore, the execution of this. onTimeoutEvent method fails because it cannot access the this. currentlyExecuting attribute.
* After bind is used, this method can find this correctly, that is, the current instance of PeriodicalExecuter.
*/
Var PeriodicalExecuter = Class. create ();
PeriodicalExecuter. prototype = {
Initialize: function (callback, frequency ){
This. callback = callback;
This. frequency = frequency;
This. currentlyExecuting = false;

This. registerCallback ();
},

RegisterCallback: function (){
SetTimeout (this. onTimerEvent. bind (this), this. frequency * 1000 );
},

OnTimerEvent: function (){
If (! This. currentlyExecuting ){
Try {
This. currentlyExecuting = true;
This. callback ();
} Finally {
This. currentlyExecuting = false;
}
}

This. registerCallback ();
}
}


For details about what is done behind Class. create (), let's take a look at the implementation of Class.

The Code is as follows:



/**
* Create a type. Note that the "create" attribute is a method and a constructor is returned.
* The following is generally used:
* Var X = Class. create (); returns a type similar to a java Class instance.
* To use the X type, you must continue to use new X () to obtain an instance, just like the java Class. newInstance () method.
*
* The returned constructor executes the initialize method, which is the name of the constructor method of the Ruby object.
* At this time, the initialize method is not defined. When a new type is created in the code, a method with the same name is created.
*/
Var Class = {
Create: function (){
Return function (){
This. initialize. apply (this, arguments );
}
}
}


Class. create actually returns a function. So what does it do when it is new. See MDN

When the code new foo (...) is executed, the following things happen:

A new object is created, inheriting from foo. prototype.
The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo (), I. e. if no argument list is specified, foo is called without arguments.
The object returned by the constructor function becomes the result of the whole new expression. if the constructor function doesn' t explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process .)
When new, the returned function is executed, that is, this. initialize. apply (this, arguments); at this time, this is the newly generated object, which means that all the initialization work of all objects is delegated to the initialize function.

-------

Why should I bind my initialize method to myself ??
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.