JS prototype intercepting string function _javascript skills

Source: Internet
Author: User
Prototype It is written in JavaScript some of the APIs, including JavaScript in the class such as String,array, such as the expansion of the JS file embedded directly with the line.
Copy Code code as follows:

String.prototype.strLen = function () {
var len = 0;
for (var i = 0; i < this.length; i++) {
if (This.charcodeat (i) > 255 | | | this.charcodeat (i) < 0) Len = 2; else Len + +;
}
return Len;
}
Splits a string into characters and into an array
String.prototype.strToChars = function () {
var chars = new Array ();
for (var i = 0; i < this.length; i++) {
Chars[i] = [This.substr (i, 1), This.ischs (i)];
}
String.prototype.charsArray = chars;
return chars;
}
Determine if a character is Chinese
String.prototype.isCHS = function (i) {
if (This.charcodeat (i) > 255 | | this.charcodeat (i) < 0)
return true;
Else
return false;
}
Intercept string (from start byte to end byte)
String.prototype.subCHString = function (start, end) {
var len = 0;
var str = "";
This.strtochars ();
for (var i = 0; i < this.length; i++) {
if (This.charsarray[i][1])
Len + 2;
Else
len++;
if (end < Len)
return str;
else if (Start < Len)
str = this.charsarray[i][0];
}
return str;
}
Intercept string (intercept length bytes from start byte)
STRING.PROTOTYPE.SUBCHSTR = function (start, length) {
Return this.subchstring (start, start + length);
}
var li= document.getelementsbyname ("ListTitle");
for (Var i=0;i<li.length;i++)
{
Li[i].innerhtml=li[i].innerhtml.subchstr (0,28) + "...";
}

Prototype is used to inherit from a prototype object, primarily to conserve memory space.

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.