当前位置: 首页 > 编程日记 > 正文

The Apply method of function object

2019独角兽企业重金招聘Python工程师标准>>> 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:

<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

相关文章:

资源 | 100+个自然语言处理数据集大放送,再不愁找不到数据!

奉上100多个按字母顺序排列的开源自然语言处理文本数据集列表&#xff08;原始未结构化的文本数据&#xff09;&#xff0c;快去按图索骥下载数据自己研究吧&#xff01; 数据集 Apache软件基金会公开邮件档案&#xff1a;截止到2011年7月11日全部公开可用的Apache软件基金会邮…

Java中ArrayList源码分析

一、简介 ArrayList是一个数组队列&#xff0c;相当于动态数组。每个ArrayList实例都有自己的容量&#xff0c;该容量至少和所存储数据的个数一样大小&#xff0c;在每次添加数据时&#xff0c;它会使用ensureCapacity()保证容量能容纳所有数据。 1.1、ArrayList 的继承与实现接…

介绍三种绘制时间线图的方法

作者 |周萝卜来源 |萝卜大杂烩今天我们再来分享几种不同的制作方法&#xff0c;大家可以自行比较下各种方法的优劣。Matplotlib 制作Matplotlib 作为 Python 家族最为重要的可视化工具&#xff0c;其基本的 API 以及绘制流程还是需要掌握的。尤其是该库的灵活程度以及作为众多工…

phpize是什么

安装php&#xff08;fastcgi模式&#xff09;的时候&#xff0c;常常有这样一句命令&#xff1a;/usr/local/webserver/php/bin/phpize 一、phpize是干嘛的&#xff1f; phpize是什么东西呢&#xff1f;php官方的说明&#xff1a; http://php.net/manual/en/install.pecl.phpiz…

C语言比较好的风格梳理

errno int err;tb malloc(sizeof(struct xtracer_table));if (!tb) {err errno;fprintf(stderr, "%s:%d, errno:%d, %s\n",__func__, __LINE__, err, strerror(err));return NULL;} 转载于:https://www.cnblogs.com/muahao/p/8979144.html

Linux下Memcache服务器端的安装

Linux下Memcache服务器端的安装服务器端主要是安装memcache服务器端&#xff0c;目前的最新版本是 memcached-1.3.0 。下载&#xff1a;http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz http://memcached.googlecode.com/files/memcached-1.4.9.tar.gz 另外&#…

如何用技术恢复模糊的图像?在线教学…

作者 |小白 来源 |小白学视觉 有人认为恢复模糊的图像是不可能的&#xff0c;因为会丢失信息。但我对这个问题进行了很多思考&#xff0c;并认为如果输出图像的大小与输入图像的大小相同&#xff0c;那实际上是可能的&#xff01;这样&#xff0c;输出就有足够的像素/信息来恢复…

数据库创建索引的原则

数据库建立索引的原则 铁律一&#xff1a;天下没有免费的午餐&#xff0c;使用索引是需要付出代价的 索引的优点有目共睹&#xff0c;但是&#xff0c;却很少有人关心过采用索引所需要付出的成本。若数据库管理员能够对索引所需要付出的代价有一个充分的认识&#xff0c;也就不…

Linux上实现ssh免密码登陆远程服务器

平常使用ssh登陆远程服务器时&#xff0c;都需要使用输入密码&#xff0c;希望可以实现通过密钥登陆而免除输入密码&#xff0c;从而可以为以后实现批量自动部署主机做好准备。 环境如下&#xff1a; IP地址操作系统服务器端10.0.0.10CentOS 6.5 x86客户端10.0.0.61CentOS 6.5 …

分享memcache和memcached安装过程

Memcache是什么&#xff1f; Memcache是一个自由和开放源代码、高性能、分配的内存对象缓存系统。用于加速动态web应用程序&#xff0c;减轻数据库负载。 它可以应对任意多个连接&#xff0c;使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间&#xff0c;然后建立一…

导入导出Android手机文件

1、获得root权限&#xff1a;adb root&#xff1b; 如提示adbd cannot run as root in production builds&#xff0c;参见我的另一篇文章&#xff1a;http://www.cnblogs.com/hdk1993/p/4770388.html 2、设置/system为可读写&#xff1a;adb remount&#xff1b; 3、将文件复制…

GPT-3 不够 Open,BigScience 构建开放语言模型,规模小 16 倍

编译 | 禾木木 出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09; 大约一年前&#xff0c;总部位于纽约布鲁克林的自然语言处理初创公司 Hugging Face 推出了 BigScience。这是一个拥有 900 多名研究人员的国际项目&#xff0c;旨在更好地理解自然语言模型原理和提高大…

华为云微服务引擎CSE大量新特性上线,诚邀您免费体验

1、提供GO语言微服务开发框架SDK 支持插件化注册中心、多RPC协议&#xff08;已默认实现http和highway&#xff0c;可扩展&#xff09; 提供熔断降级、容错、路由管理、限流、错误注入、灰度发布等治理能力 2、提供Service Mesh商业版 支持.NET、Node.js、PHP等多语言应用…

memcache和memcached安装

首先要明确 memcache不是memcached第一步安装libevent #wget https://github.com/downloads/libevent/libevent/libevent-2.0.15-stable.tar.gz #tar libevent-2.0.15-stable.tar.gz #tar xzvf libevent-2.0.15-stable.tar.gz #cd libevent-2.0.15-stable #./configure --h…

点击按钮下载文件

RequestMapping("/download.do")public void download(HttpServletRequest request,HttpServletResponse response)throws Exception {String filePath "文件路径";FileInputStream fis null;OutputStream os null;try {fis new FileInputStream(fileP…

开源社区的危机:拒绝被“白嫖”?2大著名项目遭作者破坏

作者 | 林檎来源 | 数据实战派近日&#xff0c;一位开源开发者的故意破坏&#xff0c;再次引发了机构依赖开源库的争议。这一类开源库往往由维护者义务工作而支撑。被破坏的开源库是 Marak Squires 开发的 color.js 库和 faker.js 库。这两个库被广泛使用&#xff0c;其中不乏企…

状态和面向对象编程——1.定位步骤

定位 所有无人驾驶车要安全畅游全球&#xff0c;都必须经过一系列相同的步骤。 你一直在学习第一步&#xff1a;定位。在车辆能够安全驾驶之前&#xff0c;它们首先要使用传感器和收集的其他数据对它们所处的位置做出最佳估计。 卡尔曼滤波器 让我们来回顾一下卡尔曼滤波器对汽…

ldconfig命令详解,linux动态链接库

动态链接库管理命令 为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig.此执行程序存放在/sbin目录下. ldconfig命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍…

用于自动驾驶的实时 YUV 多任务 CNN

作者 | AI 修炼之路来源 | AI 修炼之路摘要本文提出了一种针对低功耗车用SoC优化的多任务卷积神经网络(CNN)结构。我们介绍了一个基于统一架构的网络&#xff0c;其中编码器由检测和分割两个任务共享。该网络以25FPS运行&#xff0c;分辨率为1280800。简要讨论了直接利用原生YU…

博客5:文件,目录以及用户的权限管理

linux用户与组的相关内容简介&#xff1a; 1.Linux用户&#xff1a;Username/UID管理员&#xff1a;root&#xff0c;0普通用户&#xff1a;1-65535系统用户&#xff1a;1-499&#xff08;在centos7上为1-999&#xff09;作用&#xff1a;对守护进程获取资源进行权限分配登录…

以太坊代币空投合约的实现

2019独角兽企业重金招聘Python工程师标准>>> 本文将介绍如何在以太坊智能合约中实现代币的空投。区块链以太坊世界中所谓空投&#xff08;airdrop&#xff09;&#xff0c;就是免费给你的区块链地址&#xff08;公钥&#xff09;发送代币。 代币空投的方式层出不穷&…

linux命令:ln 使用方法

命令&#xff1a;ln 使用方法指令名称 : ln使用权限 : 所有使用者使用方式 : ln [options] source dist&#xff0c;其中 option 的格式为 :[-bdfinsvF] [-S backup-suffix] [-V {numbered, existing, simple}][--help] [--version] [--] 说明 : Linux/Unix 档案系统中&#xf…

10 个案例分享几个 Python 可视化小技巧,助你绘制高质量图表

作者 | 俊欣来源 | 关于数据分析与可视化一般在Python当中&#xff0c;我们用于绘制图表的模块最基础的可能就是matplotlib了&#xff0c;今天小编分享几个用该模块进行可视化制作的技巧&#xff0c;帮助你绘制出更加高质量的图表。同时本篇文章的第二部分是用Python来制作可视…

(转) 地区赛获胜策略,赛前默念!

1. 比赛中评测会有些慢&#xff0c;偶尔还会碰到隔10分钟以上才返回结果的情况&#xff0c;这段时间不能等结果&#xff0c;必须开工其他题&#xff0c;如果WA&#xff0c;两道题同时做。交完每道题都要先打印。2. 比赛时发的饭不是让你当时就吃的&#xff0c;那是给你赛后吃的…

USG防火墙telnet实验

实验使用USG5500防火墙 &#xff0c;<SRG>system-view [SRG]interface g0/0/0       [SRG-GigabitEthernet0/0/0]ip address 192.168.1.1 24          接口配置地址[SRG-GigabitEthernet0/0/0]display this&#xff08;显示当前配置&#xff09; [SRG-G…

如何营造专属你的企业技术影响力氛围感?我不允许你还不知道

CSDN 推出《开发者研究与洞察》服务。基于3200万开发者的资源&#xff0c;从开发者视角出发&#xff0c;聚焦开发者“关注”、“使用”、“体验”三方面&#xff0c;帮助技术推广者打造技术品牌、优化技术产品的市场投放策略、提升技术产品的开发者使用体验&#xff0c;直接聆听…

php报错Permission denied

去apache的log下看error_log文件 #cd /usr/local/apache2/logs/ (13)Permission denied: exec of ....index.php failed加权限就可以 #chmod x index.php路径

Spring笔记——8.基于XML Schema的简化配置

我们可以使用XML Schema的配置方式来简化xml文件的配置。p&#xff1a;简化设值注入p&#xff1a;与property子元素作用相同&#xff0c;用于设值注入。若想使用p&#xff0c;则xml文件中需要引入对p的说明&#xff0c;一般自动生成的xml都会自带。xmlns:p"http://www.spr…

测试服务命名和动态注册路由的方式@Xan

2019独角兽企业重金招聘Python工程师标准>>> 1、测试服务命名&#xff1a;如不需要网关进行权限和登录验证时&#xff0c;服务名称命名后面加“tests”&#xff0c;例如&#xff1a; sysadmintests 2、动态注册路由地址&#xff1a; http://192.168.2.164:55551/sys…

POJ1386 Play on Words

题意&#xff1a;判断一些单词能不能首尾连成一体 #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <cstdio> using namespace std; int n,father[30],range[30],save[100010],in[30],out[30]; bool us…