Prototype 1.5 & scriptaculous 1.6.1 Learning notes _prototype

Source: Internet
Author: User
Tags extend
Recently do Otalk, start is based on prototype1.4, later because I added Scriptaculous 1.6.1, she asked prototype version is 1.5, so they upgraded to 1.5, Looking at the demo to learn the use of scriptaculous.
Usage later finishing, because many times in the use of the results do not let their own satisfaction, want to see the code and do not understand, after a torture, I resolved, must put scriptaculous and prototype code to see understand!

Here as my study notes, there may be no sequence of logic, until the end of learning, finishing

The first is to define the class to look at the smile of some of the teacher's introduction, their own look is to experiment, often a lot of things to see understand, in fact, still a lot worse

var Class = {
Create:function () {
return function () {
This.initialize.apply (this, arguments);
}
}
}

Defines a class function as a template for creating a class, or a prototype.
How to use
var llinzzi= class.create ();
Llinzzi.prototype = {
Initialize:function () {
document.write (' instance is created ');
},
Fun1:function () {document.write (' method is invoked by instance ');}
}

var linchild = new Llinzzi ();
Run, the output ' instance is created ' to indicate that initialize was invoked when the instance was created
Review the Class Code
return function () {
This.initialize.apply (this, arguments);
}
You can see that when you execute the Create method, you start the call.
Linchild.fun1 ();
Output ' method is invoked by instance ', the Fun1 method was successfully invoked
When the prototype Class.create () method is used to create the object, initialize as a special method is executed when the instance is created to initialize.

Continued commitment
Object.extend = function (destination, source) {
For (Var property in source) {
Destination[property] = Source[property];
}
return destination;
}

Usage
Object.extend (target, source);
What makes me curious is that a piece of code in the Scriptaculous
var options = Object.extend ({
Greedy:true,
Hoverclass:null,
Tree:false
}, Arguments[1] | | {});
Why use the Object.extend method to define a options?
Direct
var options ={
Greedy:true,
Hoverclass:null,
Tree:false
}
No, it's all right? Wait, there's a problem. Back there arguments[1] | | {}, this should be the target, the goal is the function of the parameters, analysis, get the parameters, if not this parameter is {} is afraid, if there is, compared to {hoverclass: ' xx '} format, oh, the original definition of options is not so simple, first see there are no parameters, Whether or not, using the Object.extend method, the object in the parameter is appended or overwritten to the previous {greedy:true, Hoverclass:null, Tree:false}, if the parameter is none, it is quite Simple var options = {}; but, what if {hoverclass: ' abc '} in the argument? This time it overwrites the original Hoverclass value NULL, Then look at the Object.extend method return value is the entire value after the first argument is overwritten
Have to admire, a paragraph to define, also has set the default value.
Look more interesting, keep watching

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.