博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Apply method of function object
阅读量:6502 次
发布时间:2019-06-24

本文共 2909 字,大约阅读时间需要 9 分钟。

hot3.png

http://webreference.com

As explained in the previous page, JavaScript 1.3 includes two new methods for the Function object, call() andapply(). The apply() method is a variation on the call() method. The apply() method lets you pass the parameters from one method to the other in a different way than the call() method does it. The call() method requires the full list of parameters, as shown in the previous page:

 

exterior.call(this, extColor, doorCount, airWing, tireWidth);

 

The apply() method, on the other hand, lets you specify arguments on its second parameter:

 

exterior.apply(this, arguments);

 

What it means is that all of the caller's parameters are passed on to the callee. In the automobile assembly line from the previous page, the parameters of the caller (interior) are all the seven Volvo features:

 

intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth

 

These parameters are passed to the callee which is the exterior() method. The parameters are passed according to their position in the list. The first caller parameter is passed to the first callee parameter, the second caller parameter is passed to the second callee parameter, and so on. Since our previous exterior() method handles only the exterior features, it is not capable of handling the seven parameters. But we can easily change it by modifying the method's parameter list to include all seven options. Here is the new exterior() method:

 

function exterior(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { this.extColor = extColor; this.doorCount = doorCount; this.airWing = airWing; if (tireWidth > 10) this.wideTire = true; else this.wideTire = false; }

 

The whole script is very similar to the one presented in the previous page, except the apply's and exterior's parameters:

 

   single object constructors      

 

Now, it is very easy to add a new station to our Volvo assembly line. Let's assume a new door station has been added, and the doorCount feature is assigned in a separate constructor method:

 

function doors(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { this.doorCount = doorCount; }

 

Of course, we have to remove this assignment from the exterior method. We have now three different stations to handle. It is only natural to call all the apply() methods from a single central automobile method:

 

function automobile(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { interior.apply(this, arguments); exterior.apply(this, arguments); doors.apply(this, arguments); }

 

The whole script will look like this now:

 

   single object constructors      

 

The object-oriented structure of the script makes it easier to add more stations. Also notice that adding an automobile feature requires the extension of all parameter lists by one.

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/xiaohelong/blog/497882

你可能感兴趣的文章
亚洲黑客组织盯紧中国企业 利用小小视频攻陷高管
查看>>
导购指南:双路塔式服务器7月看点
查看>>
关于软件测试类型与归纳用例管理
查看>>
艾特网能获2016APCA用户满意品牌大奖
查看>>
《软件工艺师:专业、务实、自豪》一2.4 《敏捷软件开发宣言》
查看>>
自由软件之父 Stallman:「我一生都为使用者的自由而奋斗」
查看>>
UCKeFu 发布v2.3.0 版本,增加移动端接入渠道
查看>>
《CCNP TSHOOT 300-135学习指南》——第2章 结构化故障检测与排除进程
查看>>
《Java EE 7精粹》—— 2.5 非阻塞I/O
查看>>
《Python数据科学实践指南》一2.2 字符串
查看>>
ps命令的10个例子
查看>>
《R数据可视化手册》——1.1 安装包
查看>>
《iOS创意程序设计家》——导读
查看>>
spring-aop
查看>>
android RecycleView Adapter简单封装
查看>>
PgSQL · 案例分享 · 递归收敛优化
查看>>
Dart的数据库操作
查看>>
Codeforces 591 B Rebranding【Codeforces Round #327 (Div. 2)】
查看>>
命名难,难于上青天
查看>>
批量修改文件名后缀
查看>>