2019独角兽企业重金招聘Python工程师标准>>>

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.

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

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

The Apply method of function object相关推荐

  1. c语言 谓词,C++ 谓词(predicate) 与 仿函数 ( functor (function object))

    #谓词与函数对象 谓词 predicate C++ 标准定义谓词如下: The Predicate parameter is used whenever an algorithm expects a ...

  2. 对比学习系列论文SimROD(二): A Simple Adaptation Method for Robust Object Detection

    0.Abstract 0.1逐句翻译 This paper presents a Simple and effective unsupervised adaptation method for Rob ...

  3. TypeError: 'function' object is not subscriptable

    问题描述: 在进行网格化数据与实测数据的转换过程中,python调用numpy中的函数出现TypeError: 'function' object is not subscriptable的错误 解决 ...

  4. C++Function Object Adapter之not1

    相关博文:C++头文件<functional>和bind.placeholders占位符使用简单例子 相关博文:<Essential C++>笔记之设计一个泛型算法(二) 相关 ...

  5. 项目销毁主页Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService异常

    项目需求,项目退出登录的时候,跳转登录页面,重新登录,会重新进入首页.很简单的需求,只需在退出的时候将存储的用户信息销毁,把之前活跃的activity销毁,重新跳转进入登录界面即可.但是在真正执行退出 ...

  6. 深入探究 Function Object 鸡蛋问题 侵立删

    引言 上篇文章用图解的方式向大家介绍了原型链及其继承方案,在介绍原型链继承的过程中讲解原型链运作机制以及属性遮蔽等知识,今天这篇文章就来深入探究下 Function.__proto__ === Fun ...

  7. 【Django】‘function‘ object has no attribute ‘subjects‘报错的解决

    项目场景 创建了一个新文件夹 myapp ,用于保存新功能模块下实现功能的代码文件 views.py . 问题描述 运行项目时,报错 'function' object has no attribut ...

  8. 认识js函数对象(Function Object)

      可以用function关键字定义一个函数,对于每个函数可以为其指定一个函数名,通过函数名来进行调用.这些都是代码给用户的印象,而在JavaScript解释执行的时候,实际上每个函数都是被维护为一个 ...

  9. 15 Function Object

    简单的理解,仿函数就是一个类,这个类定义了一个 operator() 函数,使用的时候调用的就是这个函数. 特点: (1) 它可以具有函数状态(因为可以有成员变量,可以通过构造函数初始化) (2) 每 ...

最新文章

  1. 20100506 学习记录:grdview添加新的一行数据
  2. 服务容错保护断路器Hystrix之二:Hystrix工作流程解析
  3. 对ESB概念的理解(转)
  4. SQL的主键和外键约束
  5. BPMF论文辅助笔记: 固定U,更新θU 部分推导
  6. c 语言指针教学视频,[C语言] 猎豹网校 C和指针视频教程
  7. 计算机漏洞为什么无法避免,雷电3接口漏洞影响数百万计算机:5分钟解锁设备,无法修复...
  8. 【前端】h5音乐播放demo 可关闭可播放
  9. 计算机专业课如何阅卷,全国计算机等级考试评卷老师是如何阅卷的?
  10. shell脚本循环嵌套
  11. 传华为公司又一名技术部员工乔向英猝死
  12. 金九银十,果然如此,这个九月有点折腾
  13. ubuntu下全角半角切换
  14. WZOI-261疯狂吃鸡腿2
  15. 冬至到了,一碗香喷喷的饺子是否能温暖到你的胃
  16. HTML设置悬停效果,如何使用纯CSS实现按钮的悬停效果
  17. JS 逆向之 Hook
  18. 2021年A股年度行情回顾与总结
  19. 毕业一年感想~微思顾轻展望
  20. AlphaZero 五子棋实现(附完整代码)

热门文章

  1. 当年学生的一件事,心情很难过
  2. 【C++】【十一】二叉树递归遍历与非递归遍历的实现及思路
  3. Varnish 和 Squid比较到底强多少
  4. Squid的refresh_pattern配置
  5. 项目Makefile文件模板
  6. c# 中如何定义和接收消息
  7. 事务隔离机制原理分析以及是否可以防止订单超卖
  8. Linux进程浏览器htop安装与使用
  9. python error_python error整理
  10. elasticsearch 分片_Elasticsearch最佳实践之分片使用优化