Home  >  Article  >  Web Front-end  >  A brief analysis of common algorithms and functions in JavaScript_javascript skills

A brief analysis of common algorithms and functions in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:13:381040browse

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

Copy code Code As follows:

function Person()
{

}
Person.say = function(msg)
{
alert(msg);
}
Person.say("Hello");


0012:
Create an object:
Copy the code 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("Zhang San",23);
aPerson.say();


0013:
Creation of StringBuilder:
Copy code 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("世界");
aStringBuilder.append("你好");

alert(aStringBuilder.toString());


0014:
继承的实现:
复制代码 代码如下:

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("张三",25);
aMyPerson.sayName();
aMyPerson.sayAge();
aMyPerson.say();


0015:
继承的实现:
复制代码 代码如下:

function Person(name)
{
this.Name = name;
this.sayName = function()
{
alert(this.Name);
}
}

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();


0016:
多重继承:
复制代码 代码如下:

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("张三",25,"男");
aMyPerson.say();


0017:
继承的实现:原型链方式,不支持有参数的构造函数和多重继承
复制代码 代码如下:

function Person()
{

}

function MyPerson()
{

}
MyPerson.prototype = new Person();//不能有参数


0018:
合理的继承机制是混合使用以上的几种方式:
复制代码 代码如下:

function Person(name)
{
this.Name = name;
this.sayName = function()
{
alert(this.Name);
}
}

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();


0019:
错误处理:
复制代码 代码如下:



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn