最近在看《PPK谈JavaScript》以及《精通JavaScript》,对这门脚本语言的理解深了很多,相信将来会变得更加的强大!
Code
//overload testing 函数重载
function sendMsg(msg,obj)
{
if(arguments.length==2)
obj.handleMsg(msg);
else
alert(msg);
}
sendMsg("Hello, World!");
sendMsg("How are you!",{
handleMsg:function(msg)
{
alert("This is a custom msg:"+msg);
}
});
//type checkging 类型检查
var strTest="test";
var aryTest=[];
alert(strTest.constructor);
alert(aryTest.constructor);
alert(typeof strTest);
alert(typeof(aryTest));
alert(aryTest.constructor==Array);
//make arry function 生成数组
function makeArray()
{
var arr=[];
for(var i=0;i<arguments.length;i++)
{
arr.push(arguments[i]);
}
return arr;
}
//privileged method 特权方法
function User(name,age)
{
var year=(new Date()).getFullYear()-age;
this.getBornYear=function(){
return year;
};
}
var user=new User("Li",23);
alert(user.getBornYear());
//overload testing 函数重载
function sendMsg(msg,obj)
{
if(arguments.length==2)
obj.handleMsg(msg);
else
alert(msg);
}
sendMsg("Hello, World!");
sendMsg("How are you!",{
handleMsg:function(msg)
{
alert("This is a custom msg:"+msg);
}
});
//type checkging 类型检查
var strTest="test";
var aryTest=[];
alert(strTest.constructor);
alert(aryTest.constructor);
alert(typeof strTest);
alert(typeof(aryTest));
alert(aryTest.constructor==Array);
//make arry function 生成数组
function makeArray()
{
var arr=[];
for(var i=0;i<arguments.length;i++)
{
arr.push(arguments[i]);
}
return arr;
}
//privileged method 特权方法
function User(name,age)
{
var year=(new Date()).getFullYear()-age;
this.getBornYear=function(){
return year;
};
}
var user=new User("Li",23);
alert(user.getBornYear());