Ajax Getting Started Guide (v)

Source: Internet
Author: User
Tags array object bind extend html tags return string tostring
An introductory guide to Ajax learning should help Ajax beginners.

Prototype base class:
1. Class.create ()
Example:
var myClass = Class.create ();

2. Object.extend (Destination,source)
Example:
var myClass = Class.create ();
Myclass.prototype = {
Initialize:function () {
},
F1:function () {
Alert ("Do F1 ()");
},
F2:function () {
Alert ("Do F2 ()");
},
Tostring:function () {
return "MyClass";
}
};
var mysubclass = Class.create ();
Object.extend (Mysubclass.prototype, Myclass.prototype);

3. Object.inspect (Object)
Returns the text description of the target object, and returns the value of Object.ToString () by default if the object does not have a inspect method defined
Example:
var myClass = Class.create ();
Myclass.prototype = {
Initialize:function () {
},
Tostring:function () {
return "MyClass";
}
};
var obj = new MyClass ();
Alert (Object.inspect (obj));
4.function.prototype.bind (object)
Returns an instance of a function with exactly the same structure as the current function object, except that the scope has been transferred to the object objects specified by the parameter
Example:
var myClass = Class.create ();
Myclass.prototype = {
Initialize:function () {
},
Name: "MyClass",
F1:function () {
Alert (THIS.name + "from F1");
}
};
var myClass2 = Class.create ();
Myclass2.prototype = {
Initialize:function () {
},
Name: "MyClass2",
F2:function () {
Alert (THIS.name + "from F2");
}
};
var obj = new MyClass ();
var obj2 = new MyClass2 ();

OBJ2.F2 = Obj.f1.bind (OBJ2);
Obj2.f2 (); Output "MyClass2 from F1"


5.function.prototype.bindaseventlistener
The same functionality as the Bind method, except that Bingasevevntlistener is used to bind events.
Example:
var watcher = Class.create ();
Watcher.prototype = {
Initialize:function (ButtonID, message) {
This.button = $ (ButtonID);
this.message = message;
Bind the button's onclick and the ShowMessage method of this object
This.button.onclick =
This.showMessage.bindAsEventListener (this);
},
Showmessage:function () {
alert (this.message);
}
};
var watcher = new Watcher (' btn ', ' clicked ');

6.PeriodicalExecuter class
Creating an instance of the Periodicalexecuter class will periodically call the specified method
function SetTime () {
$ (' Divtime '). InnerHTML = (new Date ()). toLocaleString ();
}
New Periodicalexecuter (settime, 1);
First parameter: The method invoked; second argument: how many seconds interval
String processing (String object extension)
1.String. Prototype.gsub (pattern,replacement)
Replaces the part of a string that matches all regular expressions with the specified string
Pattern: Regular Expressions
Replacement: A string used as a replacement
Example:
var str = "This is a test test";
Output "This is a new new"
Alert (Str.gsub (/test/, "new"));

2.string.prototype.truncate (length,truncation)
Truncates a string
Length: Size of string after truncation, default value is 30
Trancation: When the string is truncated, the trailing string is substituted, and the default is "..."
Example:
var str = "This is a test test";
The output "This is ..."
Alert (Str.truncate (10));
The output "This is a t ..."
Alert (Str.truncate (14));
Output "This is***"
Alert (Str.truncate (10, "* * *"));


            3.string.prototype.strip ()
                 remove white space characters before and after a string
                 Example:
                 var str= "This is a test test ";
                alert (Str.strip ( ). length);//19
                alert (str.length);//21

4.string.prototype.striptags ()
Remove all HTML and XML tags from a string
Example:
var str = "

stripTagsDemo
";
Alert (Str.striptags ());//Output "Striptagsdemo"

5.string.prototype.stripscripts ()
Remove all script tag contents of a String
Example:
var str = "This is a test

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.