Home > Article > Web Front-end > A brief analysis of common algorithms and functions in JavaScript_javascript skills
How to use the code:
0001:
Determine whether a calculation result is infinite: if(isFinite(999999999*999999999) == true)
------------------ -------------------------------------------------- ----------------------------------
0002:
Determine whether it is a number: if(isNaN("Blue ") == true), if it is not a number, it is true, if it is a number, it is false.
-------------------------------------------------- -------------------------------------------------- ---
0003:
Number conversion:
var num = 10;
alert(num.toString()) "
";//Decimal
alert (num.toString(2)) "
";//Binary
alert(num.toString(8)) "
";//Octal
alert(num.toString(16 )) "
";//Hexadecimal
-------------------------------- -------------------------------------------------- ------------------
0004:
Convert string type to numeric type: Note that parseFloat has no decimal parameters to choose from, and they are all treated as decimal.
var str = "10";
alert(parseInt(str,10));//The string is treated as decimal
alert(parseInt(str,2));//The string is treated as decimal Treat as binary
--------------------------------------------- -------------------------------------------------- -------
0005:
Forced type conversion:
var str = "10";
var num = new Number(str);
------ -------------------------------------------------- --------------------------------------------------
0006:
Basic properties and methods of the Object class: All classes inherit from Object, so they all have these properties and methods.
Attributes:
●Constructor: A reference to the function that created the object. Points to the constructor.
●Prototype: A reference to the object prototype of this object.
Method:
●HasOwnProperty(property): Determine whether the object has a certain property.
●IsPrototypeOf(aobject): Determine whether the object is the prototype of another object.
●PrototypeIsEnumerable(protype): Determine whether the attribute can be enumerated using the for...in statement.
●ToString()
●ValueOf()
--------------------------------- -------------------------------------------------- ------------------
0007:
Several ways to declare arrays:
var array1 = new Array();
var array2 = new Array("Blue","Red","Black");
var array3 = ["Blue","Red","Black"];
------------ -------------------------------------------------- ---------------------------------------------
0008:
Creation date type Method: var myDate = new Date(Date.parse("2007/1/2"));
------------------------- -------------------------------------------------- ------------------------
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 original form
var deUrl = decodeURIComponent(enUrl);//decodeURI(enUrl);
---------------------- -------------------------------------------------- ----------------------------
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);
-------------------------------------------------- --------------------------------------------------
0011:
Static method: JavaScript's static function is the function declared to the constructor
}
Person.say = function(msg)
{
alert(msg);
}
Person.say("Hello");
var aPerson = new Person("Zhang San",23);
aPerson.say();
var aStringBuilder = new StringBuilder();
aStringBuilder.append("世界");
aStringBuilder.append("你好");
alert(aStringBuilder.toString());
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("张三",25);
aMyPerson.sayName();
aMyPerson.sayAge();
aMyPerson.say();
function MyPerson(name,age)
{
Person.call(this,name);//或者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("张三",25);
aMyPerson.sayName();
aMyPerson.sayAge();
aMyPerson.say();
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("张三",25,"男");
aMyPerson.say();
}
function MyPerson()
{
}
MyPerson.prototype = new Person();//不能有参数
function MyPerson(name,age)
{
Person.call(this,name);//或者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("张三",25);
aMyPerson.sayName();
aMyPerson.sayAge();
aMyPerson.say();