文档章节

The Apply method of function object

 大东家
发布于 2015/01/13 10:28
字数 628
阅读 12
收藏 0

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:

 

<HTML> <HEAD> <TITLE> single object constructors </TITLE> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript1.3"> <!-- function exterior(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { // (The above two lines should be joined as one line. // They have been split for formatting purposes.) this.extColor = extColor; this.doorCount = doorCount; this.airWing = airWing; if (tireWidth > 10) this.wideTire = true; else this.wideTire = false; } function interior(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { // (The above two lines should be joined as one line. // They have been split for formatting purposes.) this.intColor = intColor; this.seatCoverType = seatCoverType; this.bench = benchOption; exterior.apply(this, arguments); } volvo = new interior("blue", "leather", true, "black", 4, true, 15); // --> </SCRIPT> </BODY> </HTML>

 

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:

 

<HTML> <HEAD> <TITLE> single object constructors </TITLE> </HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript1.3"> <!-- function doors(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { // (The above two lines should be joined as one line. // They have been split for formatting purposes.) this.doorCount = doorCount; } 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; } function interior(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { this.intColor = intColor; this.seatCoverType = seatCoverType; this.bench = benchOption; exterior.apply(this, arguments); doors.apply(this, arguments); } function automobile(intColor, seatCoverType, benchOption, extColor, doorCount, airWing, tireWidth ) { interior.apply(this, arguments); exterior.apply(this, arguments); doors.apply(this, arguments); } volvo = new automobile("blue", "leather", true, "black", 4, true, 15); // --> </SCRIPT> </BODY> </HTML>

 

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.

本文转载自:http://blog.csdn.net/xiaohelong2005/article/details/4380908

粉丝 22
博文 255
码字总数 111936
作品 0
长沙
项目经理
私信 提问
理解Scala的apply

理解Scala的apply Mathematicians(数学家) have their own little funny ways, so instead of saying "then we call function f passing it x as a parameter" as we programmers would s......

秋风醉了
2016/03/08
82
0
js方法和原型继承(一)

在js语言规范中并不存在方法这一概念,方便起见,将作为对象属性的函数成为方法 this引用的规则 a.在最外层代码中,this引用的是全局对象 b.在函数内,this引用根据函数调用方式不同而不同 函数...

冬瓜1
2015/09/27
83
0
Understand JavaScript Callback Functions and Use Them-回调函数

(Learn JavaScript Higher-order Functions, aka Callback Functions) In JavaScript, functions are first-class objects; that is, functions are of the type Object and they can be use......

Oscarfff
2016/08/29
12
0
从规范去看Function.prototype.apply到底是怎么工作的?

圣诞节快乐.png 今天看element-react源码的时候,又看到了这张似曾相识却又异常陌生的老面孔,那就是Function.prototype.apply()... 虽然高设,犀牛书以及你不知道的Javascrit都看过apply的原...

趁你还年轻233
2017/12/24
0
0
jQuery apply()

Sometimes deep inside a DOM traversing chain you need a reference to plain current jQuery object. My extremely simple plugin allows you in such cases to call method, which accep......

匿名
2008/09/19
3.7K
0

没有更多内容

加载失败,请刷新页面

加载更多

爬虫可以采集哪些数据?爬虫借用什么代理可以提高效率

学习爬虫的门槛非常低,特别是通过Python学习爬虫,即使是网上也能找到许多学习爬虫的方法,而且爬虫在数据采集方面效果比较好,比如可以采集几万、上百万网页数据进行分析,带来极有价值的数...

xiaotaomi
23分钟前
4
0
redis自建笔记

自建redis笔记 最近在linux安装了一下redis,特做一些笔记! 本文先单节点启动redis,然后再进行持久化配置,在次基础上,再分享搭建主从模式的配置以及Sentinel 哨兵模式及集群的搭建 单节点...

北极之北
24分钟前
4
0
没想到Spring Boot居然这么耗内存,有点惊讶

Spring Boot总体来说,搭建还是比较容易的,特别是Spring Cloud全家桶,简称亲民微服务,但在发展趋势中,容器化技术已经成熟,面对巨耗内存的Spring Boot,小公司表示用不起。如今,很多刚诞...

程序员修BUG
28分钟前
4
0
Spring Security 实战干货:Spring Boot 中的 Spring Security 自动配置初探

1. 前言 我们在前几篇对 Spring Security 的用户信息管理机制,密码机制进行了探讨。我们发现 Spring Security Starter相关的 Servlet 自动配置都在spring-boot-autoconfigure-2.1.9.RELEASE...

码农小胖哥
30分钟前
4
0
Docker 容器时区时间不一致 问题解决

解决方案: 1,最傻瓜也最方便的处理方式,运行新的容器前设置本机时区和时间文件与容器的映射 docker run -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime ...1 -v /etc/...

突突突酱
31分钟前
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部