This article mainly introduces common algorithms and functions in JavaScript. For more information, see.
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 () +"
"; // Decimal
Alert (num. toString (2) +"
"; // Binary
Alert (num. toString (8) +"
"; // Octal
Alert (num. toString (16) +"
"; // 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
The Code is as follows:
Function Person ()
{
}
Person. say = function (msg)
{
Alert (msg );
}
Person. say ("hello ");
0012:
Create an object:
The 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:
The 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:
The 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:
The 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:
The 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
The 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:
The 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:
The Code is as follows: