Common JavaScript algorithms and functions

Source: Internet
Author: User

Code usage:

0001:
Determines whether a computing result is infinite: if (isFinite (999999999*999999999) = true)
Bytes ----------------------------------------------------------------------------------------------------
0002:
Determine if it is a number: if (isNaN ("Blue") = true), true if it is not a number, false if it is a number.
Bytes ----------------------------------------------------------------------------------------------------
0003:
Number hexadecimal conversion:
Var num = 10;
Alert (num. toString () + "<br>"; // decimal
Alert (num. toString (2) + "<br>"; // binary
Alert (num. toString (8) + "<br>"; // octal
Alert (num. toString (16) + "<br>"; // hexadecimal
Bytes ----------------------------------------------------------------------------------------------------
0004:
Convert string type to numeric type: note that parseFloat can be selected as decimal if there is no hexadecimal parameter.
Var str = "10 ";
Alert (parseInt (str, 10); // the string is processed in decimal format.
Alert (parseInt (str, 2); // the string is treated as binary.
Bytes ----------------------------------------------------------------------------------------------------
0005:
Forced type conversion:
Var str = "10 ";
Var num = new Number (str );
Bytes ----------------------------------------------------------------------------------------------------
0006:
Basic Attributes and methods of Object classes: All classes are inherited from objects, so these attributes and methods are available.
Attribute:
● Constructor: a reference to the function used to create an object. Point to the constructor.
● Prototype: Reference to the object Prototype of the object.
Method:
● HasOwnProperty (property): determines whether an object has an attribute.
● IsPrototypeOf (aobject): determines whether the object is a prototype of another object.
● PrototypeIsEnumerable (protype): determines whether attributes can be enumerated using the for... in statement.
● ToString ()
● ValueOf ()
Bytes ----------------------------------------------------------------------------------------------------
0007:
Several Methods to declare an array:
Var array1 = new Array ();
Var array2 = new Array ("Blue", "Red", "Black ");
Var array3 = ["Blue", "Red", "Black"];
Bytes ----------------------------------------------------------------------------------------------------
0008:
Method for creating the Date type: var myDate = new Date (Date. parse ("2007/1/2 "));
Bytes ----------------------------------------------------------------------------------------------------
0009:
URL encoding and decoding:
Var url = new String ("http://www.qpsh.com? Name = smartkernel ");
// Encoding: encode all non-standard characters
Var enUrl = encodeURIComponent (url); // encodeURI (url );
// Decoding: convert to the original format
Var deUrl = decodeURIComponent (enUrl); // decodeURI (enUrl );
Bytes ----------------------------------------------------------------------------------------------------
0010:
Encoding and decoding in ASP. Net:
String url = "http://www.126.com? Name = smartkernel ";
String enUrl = this. Server. HtmlEncode (url );
String deUrl = this. Server. HtmlDecode (enUrl );
Bytes ----------------------------------------------------------------------------------------------------
0011:
Static Method: JavaScript static function, that is, the function declared to the constructor
Copy codeThe Code is as follows:
Function Person ()
{

}
Person. say = function (msg)
{
Alert (msg );
}
Person. say ("hello ");

0012:
Create an object:
Copy codeThe Code is as follows:
Function Person (name, age)
{
Person. prototype. Name = name;
Person. prototype. Age = age;
// This. Name = name;
// This. Age = age;
}
Person. prototype. say = function ()
{
Alert (this. Name + "|" + this. Age );
}

Var aPerson = new Person ("James", 23 );
APerson. say ();

0013:
Create StringBuilder:
Copy codeThe Code is as follows:
Function StringBuilder ()
{
This. arrayData = new Array ();
}
StringBuilder. prototype. append = function (str)
{
This. arrayData. push (str );
}
StringBuilder. prototype. toString = function ()
{
Return this. arrayData. join ("");
}

Var aStringBuilder = new StringBuilder ();
AStringBuilder. append ("world ");
AStringBuilder. append ("hello ");

Alert (aStringBuilder. toString ());

0014:
Inheritance implementation:
Copy codeThe Code is as follows:
Function Person (name)
{
This. Name = name;
This. sayName = function ()
{
Alert (this. Name );
}
}

Function MyPerson (name, age)
{
This. ctorFun = Person;
This. ctorFun (name );
Delete this. ctorFun;

This. Age = age;
This. sayAge = function ()
{
Alert (this. Age );
}

This. say = function ()
{
Alert (this. Name + "|" + this. Age );
}
}

Var aMyPerson = new MyPerson ("James", 25 );
AMyPerson. sayName ();
AMyPerson. sayAge ();
AMyPerson. say ();

0015:
Inheritance implementation:
Copy codeThe Code is as follows:
Function Person (name)
{
This. Name = name;
This. sayName = function ()
{
Alert (this. Name );
}
}

Function MyPerson (name, age)
{
Person. call (this, name); // or Person. apply (this, new Array (name ));

This. Age = age;
This. sayAge = function ()
{
Alert (this. Age );
}

This. say = function ()
{
Alert (this. Name + "|" + this. Age );
}
}

Var aMyPerson = new MyPerson ("James", 25 );
AMyPerson. sayName ();
AMyPerson. sayAge ();
AMyPerson. say ();

0016:
Multiple inheritance:
Copy codeThe Code is as follows:
Function Person1 (name)
{
This. Name = name;
This. sayName = function ()
{
Alert (this. Name );
}
}
Function Person2 (sex)
{
This. Sex = sex;
This. saySex = function ()
{
Alert (this. sex );
}
}

Function MyPerson (name, age, sex)
{
Person1.call (this, name );
Person2.call (this, sex );

This. Age = age;
This. sayAge = function ()
{
Alert (this. Age );
}

This. say = function ()
{
Alert (this. Name + "|" + this. Age + "|" + this. Sex );
}
}

Var aMyPerson = new MyPerson ("James", 25, "male ");
AMyPerson. say ();

0017:
Implementation of inheritance: The prototype chain method does not support constructors with parameters and multiple inheritance
Copy codeThe Code is as follows:
Function Person ()
{

}

Function MyPerson ()
{

}
MyPerson. prototype = new Person (); // parameters are not allowed

0018:
A reasonable inheritance mechanism is a combination of the above methods:
Copy codeThe Code is as follows:
Function Person (name)
{
This. Name = name;
This. sayName = function ()
{
Alert (this. Name );
}
}

Function MyPerson (name, age)
{
Person. call (this, name); // or Person. apply (this, new Array (name ));

This. Age = age;
This. sayAge = function ()
{
Alert (this. Age );
}

This. say = function ()
{
Alert (this. Name + "|" + this. Age );
}
}
MyPerson. prototype = new Person ();

Var aMyPerson = new MyPerson ("James", 25 );
AMyPerson. sayName ();
AMyPerson. sayAge ();
AMyPerson. say ();

0019:
Error handling:
Copy codeThe Code is as follows:
<Head>
<Script type = "text/Javascript">
Window. onerror = function (msg, url, line)
{
Var err = "error message:" + msg + ". /N "+" error address: "+ url + ". /N "+" error row: "+ line + ". /N ";
Alert (err );
}
</Script>

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.