j**ascript 面向对象编程  function是方法(函数)

时间:2016.04.18 发布人:医生dongdao_2

j**ascript 面向对象编程  function是方法(函数)

已解决问题

谷歌医生dongdao_2用户在2016.04.18提交了关于“巴金j**ascript 面向对象编程  function是方法(函数)”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-27T14:40:10。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,没有什么华丽的语言,但是我对你的感谢不会减少 !

希望以下的回答,能够帮助你。

第1个回答

用户名:liqg08161118  

好程序不是写给computer的,而是写给human的。遇到复杂功能,应该想着把它简化、组件化来自,把小功能封装成小组件,小功能块可以任意的组合得到千问答变万化的复杂功能。function就可以帮助我们把功能进行封装。那什么是封装呢。要我说,只要把具体实现给打包,对外提供调用接口那就是封装,方法也好、类也好就做了这些事。

j**ascript中的function可以用来创建方法、也可以用来创建类,实际上我们可以认为是用function来模拟出的类(说到类一般都会要去了解闭包的知识)。还是先看一下方法吧。

j**ascript函数分为有名函数、匿名函数和在匿名函数基础上延伸出来的立即执行函数。

普通函数就是用fun当说调粮民下顾ction直接声明的有香己控村罗顶宪名函数。

functionh**ello(){alert("hello,everybody!");};h**ello();functionSayh**elloTo(some种作松body){alert("hello,"+somebody+"!");};Sayh**elloTo(川为强药易答"张三");

.csharpc指年茶财却火拿阶属ode,.csharpcodepre{font-size:s花代强它mall;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.c肉组振目蛋步父较战sharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpc初ode.attr{color:#ff0000;}.csharpcode.alt{backgroun冲承南笔尔验d-color:#f4f4f顾福字4;width:100%;ma今rgin:0em;}.csharpcode.l**m{color:#606060;}上面分别创财烟适盟预得族燃因建了h**ello和Sayh**elloTo方法。h**el天兴跳集lo不带有参数,直接通万除白果曾远过h**ello()来完成调用。Sayh**elloT充唱车附宣建酸风扬限运o方法带有一个参数,向谁问候时需要知道是在问候谁。在调用Sayh**elloTo(“张三”)湿传入参数。这些代码和j**a、C#都怎将冲略载船王其她没有什么太大区别。在方法重载上却有较大改变,j**ascript本身并不支持什么重载,一个方法名就对应一个方法。如果强制的写出多个同名方法,其实会次管出现先写的方法被覆盖掉的情况。

functionh**ello(){alert("hello,everybody!");};h**ello();functionh**ello(somebody){alert("hello,"+somebody+"!");};h**ello("张三");.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

第一个h**ello方法被覆盖掉,执行时直接调用h**ello()则认为调用第二个h**ello方法但没有传递参数值,所以出了undefined信息。调用h**ello(“张三”)时很正常的完成执行。其实j**ascript也可以用一些直白的方式来完成重载。学过C#的人都会知道有个params关键字,通过它可以实现向方法传递不定个数的参数。我们可以通过对参数的信息做手动的判断也可以模拟出类似重载的效果。而在j**ascript中根本就不需要什么params关键字,就可以很自然的实现任意个数参数的传递。function中有个arguments属性,可以把它看成一个数组,它按传递进来的参数的顺序来保存所有的参数。也就是说我们在定义方法时可以不声明参数名。

functionShowArguments(){varargs="";for(vari=0;i<arguments.length;i++){args+=arguments[i]+",";};alert(args.substr(0,args.length-1));};ShowArguments(1,2,3,4,5,6,7);.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}


试着用argements来模拟一下重载。

functionh**ello(){if(arguments.length==0){alert("hello,everybody!");}else{alert("hello,"+arguments[0]+"!");};};h**ello();h**ello("张三");

.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}基于参数个数不同的重载。

functionIncrease(arg){if(typeofarg=="undefined"){alert("请输入参数");}if(typeofarg=="string"){alert(String.fromCharCode(arg.charCodeAt(0)+1));}if(typeofarg=="**mber"){alert(arg+1);}};Increase();Increase("a");Increase(1);.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}基于参数类型不同的重载。

函数除了有名函数之外也可以是匿名函数,匿名函数就是没有名子的函数,不论函数有名还是没有名子,都是一个完整的函数对象。匿名函数还是用function来声明,但不用为它指定名称。其它的方面,比如参数等等和有名函数没什么区别。

function(){……};.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

匿名函数一般可以满足临时的函数需求,不需要有变量对其进行引用(有名的函数可以认为是有变量引用的函数)。比如需要一个函数做为衷象做为参数传入方法、需要编程的方式为对象添加事件,用匿名函数都可以很好的完成。当然你也可以单独声明变量来引用某个匿名函数对象,这和普通有名函数就没什么区别了。

functionEach(array,fun){for(vari=0;i<array.length;i++){fun(array[i]);};};var**ms=[1,2,3,4,5,6,7];Each(**ms,function(arg){alert(arg);});.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

上面代码执行,依次输出数组中的元素。

//在窗体加载时,在标题上显示当前时间window.onload=function(){document.title=newDate().toString();};//也可以将匿名方法传入定时器中setInterval(function(){document.title=newDate().toString();},1000);.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

使用匿名函数绑定事件和进行定时操作。

varh**ello=function(){alert("hello,everybody!");};.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

如果将匿名函数赋给变量,那和有名的普通函数就没区别了。但不管是变量引用还是普通地有名函数,这样的函数在内存上都持久的占有一定资源。有时候我们只想执行一次大不必使用有引用的函数,直接执行匿名函数可能是最好的选择。把匿名函数包起来,加个括号执行,一切ok,这就是由匿名函数延伸出来的立即执行函数。

(function(){alert("hello,everybody!");})();(function(somebody){alert("hello,"+somebody+"!");})("张三");.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

立即执行函数在做事件绑定,设置回调函数等方面往往会有意想不到的效果,可以解决诸如对象引用等问题。

varstudent={Name:"张三",Age:20,Intro**ce:function(){alert("我叫"+***.Name+",今年"+this.Age+"岁了!");}};window.onload=(function(obj){returnfunction(){obj.Intro**ce();};})(student);.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

因为j**ascript中函数的这些特点加之它的对象的特征,我们还可以写出一些有functional意味的程序出来。其实j**ascript中function真的是老大。

functionSum(fun,x){if(x<=0)return0;returnfun(x)+Sum(fun,x-1);};alert(Sum(function(i){returni*i;},100));.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

下面这又是什么呢?是方法吗?是类吗?

functionPoint(){};.csharpcode,.csharpcodepre{font-size:**all;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.l**m{color:#606060;}

先啰嗦到这,下次再看看类。