String. prototype usage in JavaScript

Source: Internet
Author: User

String. prototype usage in JavaScript

This example describes the usage of String. prototype in JavaScript. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

// Returns the length of two characters in a Chinese character.

String. prototype. ChineseLength = function ()

{

Return this. replace (/[^ \ x00-\ xff]/g, "**"). length;

}

// Determine whether the string ends with the specified string

String. prototype. EndsWith = function (str)

{

Return this. substr (this. length-str. length) = str;

}

// Remove the white spaces at the left end of the character

String. prototype. LeftTrim = function ()

{

Return this. replace (/(^ [\ s] *)/g ,"");

}

// Remove the white spaces on the right

String. prototype. RightTrim = function ()

{

Return this. replace (/([\ s] * $)/g ,"");

}

// Determine whether the string starts with the specified string

String. prototype. StartsWith = function (str)

{

Return this. substr (0, str. length) = str;

}

// Remove the white spaces at both ends of the character

String. prototype. Trim = function ()

{

Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");

}

This is what we often see as a technique for adding other attributes and methods to internal objects, such as String and Math. you can also use prototype to reload the attributes and methods of any internal and custom objects. when calling and executing, it will call the methods and attributes you have defined. the following is an example:

?

1

2

3

4

5

6

7

8

9

10

11

12

// Add method for internal String object

String. prototype. myMethod = function (){

Return "my define method ";

}

// Overload method for the internal String object

String. prototype. toString = function (){

Return "my define toString method ";

}

Var myObj = new String ("foo ");

Alert (myObj. myMethod ());

Alert (myObj );

Alert ("foo". toString ());

Note that all prototype attributes in JavaScript are read-only. you can add or reload properties and methods for the internal object prototype as above, but you cannot change the prototype of the internal object. however, a custom object can be assigned to a new prototype. that is to say, this is meaningless.

?

1

2

3

4

5

6

Function Employee (){

This. dept = "HR ";

This. manager = "John Johnson ";

}

String. prototype = new Employee;

Var myString = new String ("foo ");

The above program will not report an error after running, but obviously, if you call myString. dept, you will get an undefined value.

In addition, an isPrototypeOf () method under prototype is often used to determine whether the specified object exists in the prototype chain of another object. The syntax is as follows:

?

1

Object1.prototype. isPrototypeOf (0bject2 );

The preceding format is used to determine whether Object2 has an Object1 prototype chain. The example is as follows:

?

1

2

3

4

5

6

7

8

9

10

Function Person (){

This. name = "Rob Roberts ";

This. age = 31;

}

Function Employee (){

This. dept = "HR ";

This. manager = "John Johnson ";

}

Employee. prototype = new Person ();

Var Ken = new Employee ();

If you execute Employee. prototype. isPrototypeOf (Ken), Person. prototype. isPrototypeOf (Ken), and Object. prototype. isPrototypeOf (Ken), true is returned.

I hope this article will help you design javascript programs.

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.