浅析JavaScript中的常用算法与函数
人气:0代码使用方法:
0001:
判断一个计算结果是不是无穷大:if(isFinite(999999999*999999999) == true)
----------------------------------------------------------------------------------------------------
0002:
判断是不是数字:if(isNaN("Blue") == true),不是数字则为true,是数字则为false。
----------------------------------------------------------------------------------------------------
0003:
数字的进制转换:
var num = 10;
alert(num.toString()) + "<br>";//十进制
alert(num.toString(2)) + "<br>";//二进制
alert(num.toString(8)) + "<br>";//八进制
alert(num.toString(16)) + "<br>";//十六进制
----------------------------------------------------------------------------------------------------
0004:
将字符串类型转换为数字类型:注意,parseFloat没有进制参数可以选择,都当作十进制处理
var str = "10";
alert(parseInt(str,10));//字符串被当作十进制处理
alert(parseInt(str,2));//字符串被当作二进制处理
----------------------------------------------------------------------------------------------------
0005:
强制类型转换:
var str = "10";
var num = new Number(str);
----------------------------------------------------------------------------------------------------
0006:
Object类的基本属性和方法:所有类都继承自Object,因此都有这些属性和方法。
属性:
●Constructor:对创建对象的函数的引用。指向构造函数。
●Prototype:对该对象的对象原型的引用。
方法:
●HasOwnProperty(property):判断对象是不是有某个属性。
●IsPrototypeOf(aobject):判断该对象是不是另一个对象的原型。
●PrototypeIsEnumerable(protype):判断属性是否可以用for...in语句枚举。
●ToString()
●ValueOf()
----------------------------------------------------------------------------------------------------
0007:
声明数组的几种方法:
var array1 = new Array();
var array2 = new Array("Blue","Red","Black");
var array3 = ["Blue","Red","Black"];
----------------------------------------------------------------------------------------------------
0008:
创建日期类型的方法:var myDate = new Date(Date.parse("2007/1/2"));
----------------------------------------------------------------------------------------------------
0009:
URL的编码和解码:
var url = new String("http://www.qpsh.com?name=smartkernel");
//编码:将非标准字符全部编码
var enUrl = encodeURIComponent(url);//encodeURI(url);
//解码:转换为原始形式
var deUrl = decodeURIComponent(enUrl);/https://img.qb5200.com/download-x/decodeURI(enUrl);
----------------------------------------------------------------------------------------------------
0010:
ASP.Net中的编码和解码:
string url = "http://www.126.com?name=smartkernel";
string enUrl = this.Server.HtmlEncode(url);
string deUrl = this.Server.HtmlDecode(enUrl);
----------------------------------------------------------------------------------------------------
0011:
静态方法:JavaScript的静态函数,就是声明给构造函数的函数
function Person()
{
}
Person.say = function(msg)
{
alert(msg);
}
Person.say("你好");
0012:
创建一个对象:
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("张三",23);
aPerson.say();
0013:
StringBuilder的创建:
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:
错误处理:
<head>
<script type="text/Javascript">
window.onerror = function(msg,url,line)
{
var err = "错误信息:" + msg + "。/n" + "错误地址:" + url + "。/n" + "错误行数:" + line + "。/n";
alert(err);
}
</script>
加载全部内容