String.prototype usage examples _javascript techniques in JavaScript

Source: Internet
Author: User

This example describes the String.prototype usage in JavaScript. Share to everyone for your reference. Specifically as follows:

Returns the length of the character, one Chinese count 2
string.prototype.chineselength=function ()
{return 
  this.replace (/[^\x00-\xff]/g, " * * "). length;
}
Determines whether the string ends with the specified string
String.prototype.EndsWith = function (str) 
{return
  this.substr (This.length- str.length) = = str;
}
Remove the whitespace character of the Fu Zuotun
String.prototype.LeftTrim = function ()
{return
  this.replace (/(^[\\s]*)/g, "");
}
//Remove the whitespace characters
from the Fu Yi end of the word String.prototype.RightTrim = function ()
{return
  this.replace (/[\\s]*$)/g, "");
}
Determines whether the string starts with the specified string
String.prototype.StartsWith = function (str) 
{return
  this.substr (0, str.length) = = str;
}
Remove whitespace characters at both ends of the character
String.prototype.Trim = function ()
{return
  this.replace (^\s*) | ( \s*$)/g, "");

This is what we often see as the technique of adding other properties and methods to internal objects, such as String, Math, and so on. For any internal object or custom object, you can also overload its properties and methods by prototype. Then when the call executes, it invokes the method and the property you defined. Here is an example:

Add a method to the internal String object
String.prototype.myMethod = function () {return
 ' my define methods ';
}
Overloaded method
String.prototype.toString = function () {return ' My define toString methods ' for internal String objects
 ;
}
var myobj = new String ("foo");
Alert (Myobj.mymethod ());
alert (myobj);
Alert ("foo". toString ());

Also note that all JavaScript internal pairs of prototype properties are read-only. You can add or overload properties and methods for the prototype of an internal object as described above, but you cannot change the prototype prototype of the inner object. However, custom objects can be assigned to a new prototype. In other words, it's not interesting to do something like this below.

function Employee () {
 this.dept = "HR";
 This.manager = "John Johnson";
}
String.prototype = new Employee;
var myString = new String ("foo");

The above program will not make an error after running, but obviously, if you call Mystring.dept, you will get a value that is not defined.

In addition, one frequently used is the isprototypeof () method under prototype, which is used primarily to determine whether the specified object exists in the prototype chain of another object. The syntax is as follows:

Object1.prototype.isPrototypeOf (0BJECT2);

The above format is used to determine whether OBJECT2 appears in the prototype chain of Object1. Examples are as follows:

function person () {
 this.name = "Rob Roberson";
 This.age = to;
}
function Employee () {
 this.dept = "HR";
 This.manager = "John Johnson";
}
Employee.prototype = new Person ();
var Ken = new Employee ();

When performing Employee.prototype.isPrototypeOf (Ken), Person.prototype.isPrototypeOf (Ken) and Object.prototype.isPrototypeOf ( Ken), the result returns TRUE.

I hope this article will help you with your JavaScript programming.

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.