Example of String. prototype usage in JavaScript, javascriptprototype

Source: Internet
Author: User

Example of String. prototype usage in JavaScript, javascriptprototype

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

// Returns the length of the character. A Chinese character is counted as two strings. prototype. chineseLength = function () {return this. replace (/[^ \ x00-\ xff]/g ,"**"). length;} // determines whether the String ends with the specified String. prototype. endsWith = function (str) {return this. substr (this. length-str. length) = str;} // remove the white-space String at the left of the character. prototype. leftTrim = function () {return this. replace (/(^ [\ s] *)/g, "") ;}// remove the white-space String on the right of the character. prototype. rightTrim = function () {return this. replace (/([\ s] * $)/g, "");} // determines whether the String starts with the specified String. prototype. startsWith = function (str) {return this. substr (0, str. length) = str;} // remove the white String at both ends of the character. 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:

// Add the method String for the internal String object. prototype. myMethod = function () {return "my define method";} // reload the method String for the internal String object. 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.

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:

object1.prototype.isPrototypeOf(0bject2);

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

function Person() { this.name = "Rob Roberson"; 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.

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.