JavaScript基础函数整理汇总
这里给大家整理汇总了一些javascript的基础函数,都是比较常用和实用的。整理出来也有助于大家更好的理解javascript。
<scripttype="text/javascript">
/*创建函数和字面量函数*/
/*
functionadd(){
alert("函数创建成功")
};
vartestfunction=function(){
alert("这是一个匿名函数");
};
add(); //调用函数
testfunction();//调用字面量函数
*/
/*
vartestobj=newObject(); //创建对象
testobj.run=function(){ //给对象创建方法
alert("这是对象内部的方法");
};
testobj.run(); //调用对象的方法
*/
/*创建函数对象*/
/*
functionball(){
}
ball.name1="testing"; //给函数对象创建属性
alert(ball.name1); //访问函数属性
alert(typeofball)
*/
/*函数的引用*/
/*
functionmyFunction(message){
alert(message);
}
varptr=myFunction; //将函数的引用传递给变量
ptr("Testing"); //变量调用函数
*/
/*将函数的引用传递给对象*/
/*
functionsayName(name1){
alert(name1);
}
varobj1=newObject();
varobj2=newObject();
varobj3=newObject();
obj1.sayMyName=sayName; //将引用传递给对象的变量,形成对象的方法
obj2.sayMyName=sayName;
obj3.sayMyName=sayName;
obj1.sayMyName("张三") //调用对象的方法
obj2.sayMyName("李四");
obj3.sayMyName("王五")
*/
/*
functionadd(){
}
add.message="chaiyesong"
varptr1=add;
varptr2=add;
alert(ptr1.message);
alert(ptr2.message)
add.message="123";
alert(ptr1.message)
alert(ptr2.message)
*/
/*引用指向另一个变量*/
/*
functionadd(){
alert("one");
}
varptr=add;
ptr=function(){ //创建了另一个叫做myFunctionPtr的函数而不是修改它
alert("ptr")
}
add=function(){ //引用指向了另一个函数,修改了函数的引用
alert("two")
}
add()
ptr()
*/
/*创建函数对象*/
/*
functionBall(message){
alert(message)
}
varball0=newBall("testing")
ball0.name1="ball-0"
alert(ball0.name1)
functionBall(message){
alert(message)
}
varball0=newObject()
ball0.constuct=Ball; //将函数的引用指向了一个对象的构造器
ball0.constuct("ceshiceshiceshi") //由这个对象的构造器执行此函数
ball0.name1="tesing"
alert(ball0.name1)
functionTest(){
}
alert(Test.prototype) //共享属性
*/
/*添加共享属性*/
/*
functionFish(naem1,color1){
this.name1=naem1
this.color1=color1
}
Fish.prototype.LivesIn="water" //添加共享属性
Fish.prototype.price=20
varfish1=newFish("mackrel","gray")
varfish2=newFish("goldfish","orange");
varfish3=newFish("salmon","white");
for(vari=1;i<=3;i++)
{
varfish=eval("fish"+i); //我只是取得指向这条鱼的指针
alert(fish.name1+","+fish.color1+","+fish.LivesIn+","+fish.price);
}
*/
/*
functionEmployee(name,salary)
{
this.name=name;
this.salary=salary;
}
Employee.prototype.getSalary=functiongetSalaryFunction()
{
returnthis.salary;
}
Employee.prototype.addSalary=functionaddSalaryFunction(addition)
{
this.salary=this.salary+addition;
}
varboss1=newEmployee("Joan",200000);
varboss2=newEmployee("Kim",100000);
varboss3=newEmployee("Sam",150000);
alert(boss1.getSalary()); //输出200000
alert(boss2.getSalary()); //输出100000
alert(boss3.getSalary()); //输出150000
*/
/*匿名函数*/
/*
(function(x,y){
alert(x+y)
})(2,3)//这个函数自我执行的能力
*/
/*执行并且调用函数*/
/*
varf1=function(){
return"testing"
}
alert(f1())
*/
/*将自我执行的结果给变量*/
/*
varf2=function(){
return"ok"
}()
alert(f2)
*/
//(
// function(){
// alert("fa")
// }()
//)
/*
functionbox(){
varuser='Tt'
returnfunction(){
returnuser;
}
}
alert(box()())//调用内嵌函数
*/
//varb=box()
//alert(b())
/*通过闭包函数实行自动加*/
/*
functionbox(){
varage=100
returnfunction(){ //调用的其实是这个方法,实现了数据在内存中驻留的时间
age++
returnage;
}
}
varb=box()
alert(b())
alert(b())
alert(b())
*/
/*开始版*/
/*
functionbox(){
vararr=[]//申明一个数组
for(vari=0;i<5;i++){
arr[i]=function(){ //通过循环只是把函数赋值给了每个元素
returni;
}
}
returnarr;//返回一个数组
}
varb=box()//把返回的数组赋值给b
document.writeln("数组的长度为:"+b.length+"<br/>") //返回数组的长度
for(vari=0;i<b.length;i++){
document.writeln("匿名函数返回的值为:"+b[i]()+"<br/>")//执行此函数每个元素的值是5,因为最后一个元素的值为5
};
*/
/*改进版*/
/*
functionbox1(){
vararr1=[];
for(vari=0;i<5;i++){
arr1[i]=(function(num){//自我执行,把函数自我执行的结果赋值给了每个元素
returnnum;
})(i); //并且传参
}
returnarr1;
}
varb1=box1(); //返回时数组
for(vari=0;i<b1.length;i++){
document.writeln("改进后输出的结果为:")
document.writeln(b1[i]+"<br/>"); //这里返回的是数组,直接打印即可
}
*/
//测试版
/*
vartestarr=[];
for(vari=0;i<5;i++){
testarr[i]=function(){
returni;
}
};
document.writeln("测试板的长度为:"+testarr.length+"<br>")
for(vari=0;i<testarr.length;i++){
document.writeln("测试版第"+(i+1)+"次"+testarr[i]+"<br/>")
};
varaa=function(){
return3
}
document.writeln("测试版测试的结果"+aa)
*/
/*改进版3*/
/*
functionbox3(){
vararr3=[];
for(vari=0;i<5;i++){
arr3[i]=(function(num){
returnfunction(){
returnnum;
}
})(i);//自我执行的结果赋值给每个元素,执行完了,将其执行的结果赋值给了每个元素
}
returnarr3;
}
varb3=box3();
for(vari=0;i<b3.length;i++){
document.writeln(b3[i]()+"<br/>");
}
*/
/*js函数教程地址://http://www.cnblogs.com/ttcc/p/3763437.html*/
/*闭包中的this指定的对象是window*/
/*
varuser='TheWindow00';
varobj={
user:'TheWindow001',
getUserFunction:function(){
returnfunction(){
returnthis.user;
}
}
}
document.writeln("返回this指定的对象"+obj.getUserFunction()())
*/
//functionbox(count1){
// vari=12;
// for(vari=0;i<count1;i++){
// document.writeln(i);
// }
//}
//vari=10
//box(2)
/*闭包执行完毕,立马清除内存数据*/
/*
functionbox(count){
(function(){
for(vari=0;i<count;i++){
}
})();
document.writeln(i); //报错,无法访问
}
box(2);
*/
/*通过闭包访问隐私属性*/
/*
functionBox(){
varage=100;//私有变量
functionrun(){//私有函数
return'运行中...';
}
this.get=function(){//对外公共的特权方法
returnage+run();
};
}
varbox=newBox();
alert(box.get());
functionPerson(value){
varuser=value;//这句可以省略
this.getUser=function(){
returnuser;
};
this.setUser=function(value){
user=value;
};
}
*/
/*单例模式*/
/*
varbox=function(){
varuser='TT'; //私有变量
functionrun(){ //私有函数
return'运行中...';
}
return{
publicGo:function(){ //对外公共接口的特权方法
returnuser+run();
}
};
}();
alert(box.publicGo());
*/
/*单例模式*/
/*
functionDesk(){
//定义了一个函数
}
varbox=function(){
varuser='TT'; //私有变量
functionrun(){ //私有函数
return'运行中...';
}
vardesk=newDesk(); //实例化自定义对象
desk.publicGo=function(){
returnuser+run(); //给自定义函数定义方法
};
returndesk;
}();
alert(box.publicGo());
*/
</script>
上面就是个人整理的javascript基础函数了,小伙伴们仔细研究研究,希望大家能够喜欢